I came across a problem where I need to find array elements that do not exist in array1, however, exists in array2 using java. Unlike SQL, where people can directly use commands "like" or "not like", in java you might need to build a program from scratch. Although, a simple program, it just works. More solutions are welcome!
String[] colours = { "black", "red", "white", "yellow","magenta","mahogini"};
String[] colours2 = { "black", "red","orange","yellow","Yellow","pink","purple","indigo","nuque" };
ArrayList<String> coloursExist = new ArrayList<String>();
ArrayList<String> coloursDoNotExist = new ArrayList<String>();
int unmatch=0;
int max_length= colours.length;
for (int i = 0; i < colours2.length; i++) {
for (int j = 0; j < colours.length; j++) {
if (colours[j].contains(colours2[i])) {
// System.out.println(colours2[i]+" exists in colours");
coloursExist.add(colours2[i]);
break;
}
unmatch++;
if (unmatch==max_length) {
coloursDoNotExist.add(colours2[i]);
}
}//end of inner for loop
unmatch=0;
}//end of for loop
Result:
[black, red, yellow]
[orange, Yellow, pink, purple, indigo, nuque]