I came across this problem:
Given two arrays of numbers, find if each of the two arrays have the same set of integers. Suggest an algorithm which can run faster than
N * log(N)
without extra space.
Here is the link
find-if-two-arrays-contain-the-same-set-of-integers
algorithm-to-tell-if-two-arrays-have-identical-members
But after reading all answer from above mentioned links, I didn't find this simple answer which I came across, Here it is....
int main(){
int a[] = {1,5,5,7,5,6,6};
int b[] = {1,6,6,5,7,5,9};
int i = 0;
int size = 0;
int xor_ab = a[0]^b[0];
int sumDiff_ab = (a[0] - b[0]);;
if(sizeof(a)/sizeof(a[0]) == sizeof(b)/sizeof(b[0])){
size = sizeof(a)/sizeof(a[0]);
}else{
printf("not identical : array size differ");
return 0;
}
for(i=1; i < size ; ++i){
xor_ab = xor_ab ^ a[i] ^ b[i];
sumDiff_ab += (a[i] - b[i]);
}
if(xor_ab == 0 && sumDiff_ab == 0){
printf("identical");
}else{
printf("not identical");
}
return 0;
}
Now I want to know, whether my solution will work for all use cases or not. If not, Please let me know such use cases.
[EDIT]
Please consider all numbers are +ve in array.
[Accepted Answer]
I Accepted answer of @Boris Strandjev,
My solution won't work for cases like
{3,5}
{1,7}