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()

Answer from Jigar Joshi on Stack Overflow
Top answer
1 of 2
12

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()

2 of 2
5

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++;
        }
    }
}
🌐
Baeldung
baeldung.com › home › java › java array › comparing arrays in java
Comparing Arrays in Java | Baeldung
January 8, 2024 - Having sorted the arrays in the same order first, we allow the deepEquals method to find if these two arrays are equal. Java 9 introduced a brand new method in the Arrays utility class called compare().
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-two-arrays-java
Compare Two Arrays in Java - GeeksforGeeks
July 23, 2025 - Java provides a direct method Arrays.equals() to compare two arrays. There is a list of equals() methods in the Arrays class for different primitive types (int, char etc.) and one for Object type (which is the base of all classes in Java).
🌐
DataCamp
datacamp.com › doc › java › compare
Java Array compare()
While Java does not have a built-in compare() method specifically for arrays, it provides utility methods in the java.util.Arrays class to facilitate array comparison. The Arrays.equals() method is used to compare two arrays for equality. It returns true if both arrays have the same length ...
🌐
YouTube
youtube.com › coding with john
Array vs. ArrayList in Java Tutorial - What's The Difference? - YouTube
Arrays vs. ArrayLists - What's the difference? How do you use each of them, and which one should you use and why?We'll go in-depth about the similarities and...
Published: February 24, 2021
Views: 518K
🌐
Blogger
javahungry.blogspot.com › 2015 › 03 › difference-between-array-and-arraylist-in-java-example.html
8 Difference between Array and ArrayList in Java with Example | Java Hungry
This question checks whether candidate ... between array and arraylist in java include eight points namely Resizable, Performance, Traversal ,Primitives , Length , Type-Safety, Adding elements , Multi-dimensional....
🌐
Java67
java67.com › 2012 › 12 › difference-between-array-vs-arraylist-java.html
Difference between Array vs ArrayList in Java | Java67
Both Array and Array List are used ... The main difference between Array vs ArrayList in Java is the static nature of the Array and the dynamic nature of ArrayList....
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-arrays-compare-method-with-examples
Java Arrays compare() Method with Examples - GeeksforGeeks
In this example, we will compare two integer arrays to determine their lexicographical order using the compare() method from the Arrays class. ... import java.util.Arrays; public class CompareExample{ public static void main(String[] args) { //Initialized two integer array int[] arr1 ={6, 7, 8, 11, 18, 8, 2, 5}; int[] arr2 ={3, 5, 9, 13, 28, 6, 8, 9}; //compare both integer array using compare method and finally print result System.out.println(""+ Arrays.compare(arr1,arr2)); } }
Published: July 23, 2025
🌐
Medium
medium.com › @AlexanderObregon › javas-arrays-compare-method-explained-7989daf18608
Java’s Arrays.compare() Method Explained | Medium
February 6, 2025 - If a difference is found at any position, the result depends on the first mismatched element — returning a positive or negative value based on which element is greater. For primitive arrays, compare() evaluates values using natural ordering.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › array-vs-arraylist-in-java
Array vs ArrayList in Java - GeeksforGeeks
June 5, 2026 - An array has a fixed size, while ArrayList is dynamic and part of the Java Collections Framework, offering more built-in methods for easy data manipulation. Array has a fixed length, ArrayList can grow and shrink dynamically.
🌐
Oracle
blogs.oracle.com › javamagazine › java-array-objects
Inside the JVM: Arrays and how they differ from other objects | javamagazine
June 22, 2023 - The first major difference is that arrays are created by the JVM—not by an implicit or explicit call to new() by the developer. When the Java compiler first comes upon a set of brackets attached to a variable name, it emits a specific bytecode ...
🌐
DEV Community
dev.to › aurban28 › arraysmismatch-and-arrayscompare-in-java-15ch
mismatch() and compare() in Java - DEV Community
November 30, 2024 - Here's a breakdown of how they differ: 1. Arrays.compare(T[] a, T[] b) The compare method compares two arrays lexicographically.
🌐
Baeldung
baeldung.com › home › java › java list › finding the differences between two lists in java
Finding the Differences Between Two Lists in Java | Baeldung
April 29, 2026 - List<String> differences = new ArrayList<>(listTwo); differences.removeAll(listOne); assertEquals(3, differences.size()); assertThat(differences).containsExactly("Daniel", "Alan", "George"); We should also note that if we want to find the common elements between the two lists, List also contains a retainAll method. A Java Stream can be used for performing sequential operations on data from collections, which includes filtering the differences between lists:
🌐
Oracle
blogs.oracle.com › javamagazine › java-quiz-arrays-compare-mismatch
Quiz yourself: Comparing Java arrays and interpreting the mismatch method | javamagazine
July 10, 2023 - The mismatch() method returns the index value of the first point in the arrays where the values of the elements differ. If the arrays are identical in contents and length, the method returns -1. Also, if either array reference is null, a ...
🌐
Just Academy
justacademy.co › blog-detail › difference-between-array-and-arrays-in-java
Difference Between Array And Arrays In Java by Roshan Chaturvedi | JustAcademy
Difference Between Array And Arrays In JavaIn Java, an array is a data structure that stores a fixed-size collection of elements of the same data type, while Arrays is a utility class in the java.util package that provides various static methods ...
🌐
TechVidvan
techvidvan.com › tutorials › java-array-vs-arraylist
Difference Between Array and ArrayList in Java - TechVidvan
June 17, 2020 - To use ArrayList we need to specifically import the package java.util.ArrayList, while to use array you need not import any packages in your class. ArrayList is more flexible than arrays as it comes with many inbuilt functionalities but at the same time, it has poor performance efficiency than arrays. At the end of this article, we have also discussed some important differences with the help of examples for your better understanding.
🌐
Blogger
javarevisited.blogspot.com › 2016 › 01 › 9-difference-between-array-vs-arraylist-in-java.html
Difference between a List and Array in Java? ArrayList vs Array Example
The ArrayList on the other hand is a class in the Java Collection framework which was introduced as a dynamic array. Since an array is static in nature, I mean you cannot change the size of an array once created, So, if you need an array that can resize itself then you should use the ArrayList. It's like a dynamic array that can grow itself whenever needed. This is the fundamental difference between an array and an ArrayList.
🌐
Medium
medium.com › @AlexanderObregon › java-arrays-and-arraylists-a-comparative-look-bcbc97b32a1e
Java Arrays and ArrayLists: A Comparative Look
November 18, 2023 - Java arrays offer simplicity and high performance for fixed-size collections, while ArrayLists provide flexibility and a rich suite of features for dynamic collections. Understanding their differences is key to optimizing data management in ...