I have a problem to find common elements in two arrays and that\'s of different size.
Take , Array A1
of size n
and Array A2
o
I solve the problem by using Set intersection. It is very elegant. Even though I did not analyze the time complexity, it is probably in reasonable range.
public Set FindCommonElements(Integer[] first, Integer[] second)
{
Set set1=new HashSet(Arrays.asList(first));
Set set2=new HashSet(Arrays.asList(second));
// finds intersecting elements in two sets
set1.retainAll(set2);
return set1;
}