This post will discuss how to remove duplicate arrays from a set of arrays in Java.

The arrays in Java inherit from the Object class and use the default hashCode() and equals() methods. Using arrays as keys in a HashMap or a HashSet doesn’t work as expected. This is because the hashCode() method depends on the reference and there is no way to override it. For example, consider the following code:

Download  Run Code

Output:

[D, E, F]
[A, B, C]
[A, B, C]

 
This results in a set containing three arrays: [A, B, C], [D, E, F], [A, B, C], where the array [A, B, C] is repeated. The task is to remove the duplicate arrays from the set.

1. Use a Set<List>

The recommended solution is to use Set<List> instead of a Set<String[]>, which uses a hash code derived from the its elements using the following calculation:

 
This way, we can easily eliminate the duplicate arrays by adding them to a Set of Lists. However, this may change the order of the elements in the arrays. For example, the following solution uses a Set<List> instead of a Set<String[]>:

Download  Run Code

2. Using a TreeSet

Another option is to use the TreeSet implementation of the Set interface with a custom comparator, which performs comparison between two elements using its compareTo() or compare() method. A TreeSet is a sorted set that compares its elements using a comparator. We can define our own comparator to compare two arrays for equality based on their elements. This way, we can maintain the order of the elements in the arrays and remove the duplicates by adding them to a TreeSet. For example, the following code initializes a TreeSet object with a custom comparator that compares two String arrays for equality. Now, two arrays that are deemed equal by the custom comparator are also considered the same from the standpoint of the set.

Download  Run Code

Output:

[A, B, C]
[D, E, F]

 
Instead of writing our comparator, we can also use built-in comparator Arrays::compare that compares two arrays in lexicographical order. Here is an example of applying this comparator to a set of string arrays in Java:

Download  Run Code

Output:

[A, B, C]
[D, E, F]

That’s all about removing duplicate arrays from a set of arrays in Java.