We need to check if 2 arrays are similar or not. Elements can also be duplicates. For example, A = {2,3,4,5,6,6} and B = {3,6,2,4,6,5} are similar.
I have a naive solution:
foreach i:int in arr1
foreach j:int in arr2
{
if(i == j)
j = -1;
}
Now, if all elements of j are equal to -1, we can say that 2 arrays are similar. Can someone give a test case in which this does not work (I hope it will work!)?
It is also O (n ^ 2). Can we do better? Sorting and Hashing are not allowed.
source
share