Check if two arrays are equal or not Python

PROGRAM TO CHECK IF TWO ARRAYS ARE EQUAL OR NOT



Two arrays are said to be equal if both of them contain the same set of elements, arrangements (or permutation) of elements may be different though.
Note: If there are repetitions, then counts of repeated elements must also be the same for two arrays to be equal. 


# Returns true if arr1[0..n-1] and
# arr2[0..m-1] contain same elements.
 
def areEqual(arr1, arr2, n, m):
 
    # If lengths of array are not
    # equal means array are not equal
    if (n != m):
        return False
    b1 = arr1[0]
    b2 = arr2[0]
     
    # find xor of all elements
    for i in range(1, n - 1):
        b1 ^= arr1[i]
 
    for i in range(1, m - 1):
        b2 ^= arr2[i]
 
    all_xor = b1 ^ b2
     
    # If all elements were same then xor will be zero
    if(all_xor == 0):
        return True
 
    return False
 
 
# Driver Code
arr1 = [3, 5, 2, 5, 2]
arr2 = [2, 3, 5, 5, 2]
n = len(arr1)
m = len(arr2)
 
# Function call
if (areEqual(arr1, arr2, n, m)):
    print("Yes")
else:
    print("No")


OUTPUT
Yes

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java