Comparing arrays that have same elements in different order

前端 未结 6 944
遥遥无期
遥遥无期 2020-12-17 21:26

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};
相关标签:
6条回答
  • 2020-12-17 21:38

    If you know the interval of values, you could hold an integer array with the size equal to the maximum element of the sequences. Then traverse each array and increment by 1 the number at the position corresponding to the value in the counter array. At the end, traverse the counter array and determine if all elements that are not 0 are 2. In this case the arrays are equal.

    int[] counters = new int[MAX];
    for(int i = 0; i < length1; i++)
        counters[array1[i]]++;
    for(int i = 0; i < length2; i++)
        counters[array2[i]]++;
    
    bool areEqual = true;
    for(int i = 0; i < MAX; i++)
        if(counters[i] != 0 && counters[i] != 2) 
        {
            areEqual = false;
            break;
        }
    

    This assumes there are no duplicates. If you have duplicates, then in the first two for loops add:

    for(int i = 0; i < length1; i++)
        if(counters[array1[i]] == 0)
           counters[array1[i]]++;
    

    This ensures that any duplicates are not considered after the first one.

    0 讨论(0)
  • 2020-12-17 21:38

    Try this function it return array:-

    public static String[] numSame (String[] list1, String[] list2) 
         {  
              int same = 0;  
              for (int i = 0; i <= list1.length-1; i++) 
              {  
                 for(int j = 0; j <= list2.length-1; j++) 
                 {  
                    if (list1[i].equals(list2[j])) 
                    {  
                        same++;  
                        break;  
                    }  
                 }  
              }  
    
              String [] array=new String[same];
              int p=0;
              for (int i = 0; i <= list1.length-1; i++) 
              {  
                 for(int j = 0; j <= list2.length-1; j++) 
                 {  
                    if (list1[i].equals(list2[j])) 
                    {  
                        array[p]=  list1[i]+"";
                        System.out.println("array[p] => "+array[p]);
                        p++;
                        break;  
                    }  
                 }  
              } 
              return array;
           }  
    
    0 讨论(0)
  • 2020-12-17 21:45

    You are reinventing wheel to sort arrays.

    Use

    java.util.Arrays.sort(T[] a, Comparator<? super T> c)
    

    also there are methods to sort primitive types such as int.

    0 讨论(0)
  • 2020-12-17 21:46
    private static boolean compairArraysOfDifferentSequence(Integer[] arr1, Integer[] arr2){
    
        if(arr1 == null || arr2 == null){
    
            return false;
        }
        if(arr1.length != arr2.length)
        {
            return false;
        }
        else{
    
            Arrays.sort(arr1);
            Arrays.sort(arr2);
    
            return Arrays.deepEquals(arr1, arr2);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-17 21:47

    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);
    }
    
    0 讨论(0)
  • 2020-12-17 21:52

    If you have no duplicates you can turn the arrays into sets:

    new HashSet<Integer>(Arrays.asList(arr1))
        .equals(new HashSet<Integer>(Arrays.asList(arr2)))
    

    Otherwise:

    List<Integer> l1 = new ArrayList<Integer>(Arrays.asList(arr1));
    List<Integer> l2 = new ArrayList<Integer>(Arrays.asList(arr1));
    
    Collections.sort(l1);
    Collections.sort(l2);
    
    l1.equals(l2);
    
    0 讨论(0)
提交回复
热议问题