How to compare two arrays of integers order-insensitively

前端 未结 8 1087
梦谈多话
梦谈多话 2021-01-19 13:23

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

8条回答
  •  鱼传尺愫
    2021-01-19 13:58

    if you take one array as 1,2,3,4,3,1,2,4 then the solution is in this way.

    2n = total number of integers : 8

    //program
    int i, j, n = 4;
    for(i = 0; i < n; i++)
      for(j = n; j < 2n; j++)
       {
         if( a[i] != a[j])
          { 
            j++;
          }
         else
         {
           i++; exit();
         }
      }  
    
    if (i == n) 
    { //They both are equal;
    }
    else if(i != n)
    {
    //They both are not equal;
    }
    

    If it is not working please comment on it. Thank You.

提交回复
热议问题