Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing arrays that have same elements in different order

Tags:

java

algorithm

I wrote below code to compare to arrays that have same elements but in diff order.

 Integer arr1[] = {1,4,6,7,2};
 Integer arr2[] = {1,2,7,4,6};

For example, Above arrays are equal as they same elements 1,2,4,6,7. If you have better code for larger arrays, please share.

Edit If unique elements are taken from both the arrays and if they appear to be same, then also array should be equal. How do I write the code without using any collections classes. Ex: arr1={1,2,3,1,2,3} arr2={3,2,1} Method should return true (=both arrays are same).

package com.test;

public class ArrayCompare {

public boolean compareArrays(Integer[] arr1, Integer[] arr2){
    if(arr1==null || arr2==null){
        return false;
    }
    if(arr1.length!=arr2.length){
        return false;
    }

    Integer[] sortedArr1=sortArray(arr1);
    Integer[] sortedArr2=sortArray(arr2);

    for(int i=0;i<sortedArr1.length-1;i++){
        if(sortedArr1[i]!=sortedArr2[i]){
            return false;
        }
    }
     return true;
}
public void swapElements(Integer[] arr,int pos){
    int temp=arr[pos];
    arr[pos]=arr[pos+1];
    arr[pos+1]=temp;
}
public Integer[] sortArray(Integer[] arr){
    for(int k=0;k<arr.length;k++){
        for(int i=0;i<arr.length-1;i++){
            if(arr[i]>arr[i+1]){
                swapElements(arr,i);
            }
        }
    }
    return arr;
}


public static void main(String[] args) {
    Integer arr1[] = {1,4,6,7,2};
    Integer arr2[] = {1,2,7,4,6};
    ArrayCompare arrComp=new ArrayCompare();
    System.out.println(arrComp.compareArrays(arr1, arr2));
}

}

like image 281
crazyTechie Avatar asked Nov 12 '11 09:11

crazyTechie


People also ask

What is the correct way to compare array elements in the array?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.

Can you use == to compare arrays?

While comparing two arrays we can not use “==” operator as it will compare the addresses of the memory block to which both the arrays are pointing.


1 Answers

Do you care about duplicate counts? For example, would you need to distinguish between { 1, 1, 2 } and { 1, 2, 2 }? If not, just use a HashSet:

public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
    HashSet<Integer> set1 = new HashSet<Integer>(Arrays.asList(arr1));
    HashSet<Integer> set2 = new HashSet<Integer>(Arrays.asList(arr2));
    return set1.equals(set2);
}

If you do care about duplicates, then either you could use a Multiset from Guava.

If you want to stick with the sorting version, why not use the built-in sorting algorithms instead of writing your own?

EDIT: You don't even need to create a copy, if you're happy modifying the existing arrays. For example:

public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
    Arrays.sort(arr1);
    Arrays.sort(arr2);
    return Arrays.equals(arr1, arr2);
}

You can also have an optimization for the case where the arrays aren't the same length:

public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
    // TODO: Null validation...
    if (arr1.length != arr2.length) {
        return false;
    }
    Arrays.sort(arr1);
    Arrays.sort(arr2);
    return Arrays.equals(arr1, arr2);
}
like image 168
Jon Skeet Avatar answered Nov 07 '22 23:11

Jon Skeet