Store the unique primes
int cnt [ ] = new int [ 1000009 ] ; for ( int i = 0 ; i < 1000001 ; i ++ ) cnt [ i ] = 0 ; Vector < Integer > current_set = new Vector < > ( ) ; for ( int i = 0 ; i < current . size ( ) ; i ++ ) { cnt [ ( int ) current . get ( i ) ] ++ ; if ( cnt [ ( int ) current . get ( i ) ] == 1 ) current_set . add ( ( int ) current . get ( i ) ) ; }