How to compare two arrays of integers order-insensitively

Posted by stdnoit on Stack Overflow See other posts from Stack Overflow or by stdnoit
Published on 2010-04-19T06:41:31Z Indexed on 2010/04/19 8:43 UTC
Read the original article Hit count: 213

Filed under:
|

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.

  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;
    

anything wrong with the code ? thanks

© Stack Overflow or respective owner

Related posts about java

Related posts about homework