Check if they are already present in the array or not
for ( int k = 0 ; k < n ; k ++ ) { if ( a [ k ] . equals ( new String ( p ) ) ) { flag1 = 1 ; } if ( a [ k ] . equals ( new String ( q ) ) ) { flag2 = 1 ; } }