How to compare two arrays of integers order-insensitively
- by stdnoit
I want Java code that can compare in this way (for example):
<1 2 3 4> = <3 1 2 4>
<1 2 3 4> != <3 4 1 1>
I can't use hashmap table or anything; just pure code without library.
I know there are two ways.
sort them and compare the array index by index
use two for loops and compare the outer index with the inner index. I have been trying with this but still not working:
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(a[i] != a[j] && j == n)
return false;
}
}
return true;
anything wrong with the code ? thanks