Compare 4 arrays for equality

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • capablanca
    New Member
    • May 2010
    • 60

    Compare 4 arrays for equality

    Hello C++, C Sharp
    Is there anybody can tell me if this condition is correct to compare 4 arrays for equality:

    if (A != B || B != C || C != D)
    return false;

    Thanks
  • Falkeli
    New Member
    • Feb 2012
    • 19

    #2
    In C#, this checks if the array references point to the same object, not if they are equal internally.

    What you can do is use the following function:

    Code:
    bool ArraysEqual<T>(T[] first, T[] second)
    {
        if (first==second)
            return true;
        if (first==null || second==null)
            return false;
        if (first.Length != second.Length)
            return false;
    
        for(int i=0;i<first.Length; i++)
        {
            if (first[i] != second[i])
                return false;
        }
    
        return true;
    }
    And then, your if condition will be:
    Code:
    if (!(ArraysEqual(A,B)&&ArraysEqual(B,C)&&ArraysEqual(C,D))
    This should do it.

    Comment

    • capablanca
      New Member
      • May 2010
      • 60

      #3
      Thanks Falkeli for everything, you helped me a lot, thanks again

      Comment

      Working...