# 如何對兩個整數數組進行順序不敏感比較

<1 2 3 4>  = <3 1 2 4>
<1 2 3 4> != <3 4 1 1>


1. sort them and compare the array index by index
2. 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;


## 最佳答案

Sort & compare. You can't get the complexity better than that, and any "improvement" that you do on the speed comes at the risk that your code will be wrong.

[編輯]實際上......如果你知道你的數字相對較小（例如：數組只包含0到1000之間的數字），那麽在O（n）中有另一種選擇。這樣的事情（抱歉，如果語法錯誤，我最近沒有使用java）：

int count[1001];//already intialized to 0

for(int i=0;i<=1000 && arrays_identical; i++)
arrays_identical &= count[i]==0;