Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

differences between two arrays [duplicate]

Tags:

java

arrays

Possible Duplicate:
intersection/union of arraylists in java

Hello I have two string arrays.I want to print differences between two arrrays.Is there any java method for this? For example;

String[ ] first={"A","B","C"};
String[ ] second={"C","B"};

and result must be "A". Thanks for all comments.

like image 749
ademcu Avatar asked Dec 05 '12 20:12

ademcu


People also ask

How do you find duplicates in two arrays?

You should leverage a hashmap/hashset for a substantially faster O(n) solution: void findDupes(int[] a, int[] b) { HashSet<Integer> map = new HashSet<Integer>(); for (int i : a) map. add(i); for (int i : b) { if (map. contains(i)) // found duplicate! } }

How do I compare two arrays of arrays?

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.

How can you tell if two arrays are different?

The Arrays. equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.


2 Answers

Convert array to Set<String>

new HashSet<String>(Arrays.asList(array));

and do

Set<String> commonOnes = biggerSet.retainAll(smallerSet);
biggerSet.removeAll(commonOnes).add(smallerSet.removeAll(commonOnes))

or use guava difference()

like image 100
jmj Avatar answered Nov 06 '22 07:11

jmj


This runs in O(n log n + m log m), where n is the size of first, and m is the size of second. Basically, it sorts the arrays, then pass through each one, adding ones that don't match to the LinkedList at each opportunity, then makes an array at the end. The earlier revision of this code did not work correctly because the trailing elements in the longer list were not getting added at the end.

public class SetDifference {
    public static void main(String... args) {
        String[] arrA = {"1", "2", "3", "4", "5", "25", "10"};
        String[] arrB = {"1", "2", "10", "4", "30"};

        System.out.println(Arrays.toString(differences(arrA, arrB)));
    }

    public static String[] differences(String[] first, String[] second) {
        String[] sortedFirst = Arrays.copyOf(first, first.length); // O(n)
        String[] sortedSecond = Arrays.copyOf(second, second.length); // O(m)
        Arrays.sort(sortedFirst); // O(n log n)
        Arrays.sort(sortedSecond); // O(m log m)

        int firstIndex = 0;
        int secondIndex = 0;

        LinkedList<String> diffs = new LinkedList<String>();  

        while (firstIndex < sortedFirst.length && secondIndex < sortedSecond.length) { // O(n + m)
            int compare = (int) Math.signum(sortedFirst[firstIndex].compareTo(sortedSecond[secondIndex]));

            switch(compare) {
            case -1:
                diffs.add(sortedFirst[firstIndex]);
                firstIndex++;
                break;
            case 1:
                diffs.add(sortedSecond[secondIndex]);
                secondIndex++;
                break;
            default:
                firstIndex++;
                secondIndex++;
            }
        }

        if(firstIndex < sortedFirst.length) {
            append(diffs, sortedFirst, firstIndex);
        } else if (secondIndex < sortedSecond.length) {
            append(diffs, sortedSecond, secondIndex);
        }

        String[] strDups = new String[diffs.size()];

        return diffs.toArray(strDups);
    }

    private static void append(LinkedList<String> diffs, String[] sortedArray, int index) {
        while(index < sortedArray.length) {
            diffs.add(sortedArray[index]);
            index++;
        }
    }
}
like image 45
durron597 Avatar answered Nov 06 '22 09:11

durron597