SwadhyAI Notes

All Java notes

How to Remove Duplicates from Array in Java

The Challenge

Remove duplicate elements and keep only unique values!

Solutions

Method 1: Using HashSet

java
import java.util.*;

Integer[] numbers = {1, 2, 3, 2, 4, 3, 5};
Set<Integer> uniqueSet = new LinkedHashSet<>(Arrays.asList(numbers));
Integer[] unique = uniqueSet.toArray(new Integer[0]);

System.out.println(Arrays.toString(unique));
// Output: [1, 2, 3, 4, 5]

Method 2: Using Java 8 Streams

java
int[] numbers = {1, 2, 3, 2, 4, 3, 5};
int[] unique = Arrays.stream(numbers).distinct().toArray();

System.out.println(Arrays.toString(unique));
// Output: [1, 2, 3, 4, 5]

Did You Know?

Use LinkedHashSet to maintain insertion order, or TreeSet to get sorted unique elements!

Pro Tip

The distinct() stream operation is the simplest and most readable approach for removing duplicates!

Real-World Use Cases

  • Data cleaning: Remove duplicate entries
  • User management: Ensure unique usernames
  • Analytics: Count unique visitors
›Short versionOpen if you only have a minute.
  • A set keeps one of each value. LinkedHashSet keeps the first-seen order. HashSet does not.
  • Distinct objects need equals and hashCode. Identity equality leaves equal values in place.
  • An in-place filter on a sorted array is a different contract: it rewrites the prefix and returns the new length.
  • Streams.distinct on an int array boxes, and the encounter order of a stream from an array is the array order.

Remove duplicates

Keep one occurrence of each value.

Working approach

  • Add into a LinkedHashSet when the first occurrence should stay in front.
  • Use a HashSet only when order does not matter.
  • For objects, define equals and hashCode from the fields that make two values the same.

Walk the sample

  1. Values 1, 2, 2, 3
  2. Add 1, add 2, skip the second 2, add 3
  3. The ordered unique list is 1, 2, 3

The trap

new HashSet<>(list) can change the order of the values that remain. Callers who wanted the first spelling of each word lose that order.

java
static List<String> uniqueInOrder(List<String> values) {
    return new ArrayList<>(new LinkedHashSet<>(values));
}

Why does a HashSet sometimes change which duplicate appears first?

It does not remember insertion order. Iteration follows buckets.

LinkedHashSet keeps the first insertion and ignores later equals matches. A custom class without equals and hashCode never matches a distinct instance with the same fields, so nothing is removed.

Expected time O(n), space O(n) for the set.

Easy to get wrong

  • Using HashSet when the original order is part of the result.
  • Forgetting equals and hashCode on the element type.