Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 60

Remove Duplicates from Array in Java

In this lesson: Remove duplicate elements from an array in Java using loops, sorting and Set — code with output. In Hindi & English.

The problem

An array can contain repeated values, like {1, 2, 2, 3, 4, 4, 5}. Removing duplicates means keeping only one copy of each — {1, 2, 3, 4, 5}. The cleanest way in Java uses a HashSet, which only holds unique values.

💡 A HashSet automatically drops duplicates, so it is the easiest tool for this job.

Using a HashSet

import java.util.*;

int[] arr = {1, 2, 2, 3, 4, 4, 5};
Set<Integer> unique = new HashSet<>();
for (int x : arr) {
    unique.add(x);      // duplicates ignored
}
System.out.println(unique);
[1, 2, 3, 4, 5]

Adding every element to a HashSet keeps only the distinct ones — duplicates are simply ignored. This is the shortest and fastest common approach.

Dry run

Dry Run — removing duplicates with a Set, {1,2,2,3,4,4,5}
xAlready in the Set?Set now
1no{1}
2no{1,2}
2yes{1,2} (skip)
3no{1,2,3}
4no{1,2,3,4}
4yes{1,2,3,4} (skip)
5no{1,2,3,4,5}
💡 Understand it this way

Each element is added to the Set. If it is already there (the repeated 2 and 4), the Set ignores it. Only unique values remain.

A manual approach

int[] arr = {1, 2, 2, 3};
ArrayList<Integer> result = new ArrayList<>();
for (int x : arr) {
    if (!result.contains(x)) {   // not seen yet
        result.add(x);
    }
}
System.out.println(result);
[1, 2, 3]

Without a Set, you loop and add each element only if it is not already in the results. This works but is slower (O(n²)) because contains checks every kept item each time.

Keeping the order

A plain HashSet does not keep the original order. If order matters, use a LinkedHashSet:

Set<Integer> unique = new LinkedHashSet<>();
// keeps items in the order they were first added

LinkedHashSet removes duplicates and preserves the insertion order — the best of both.

Common mistakes

  • Expecting HashSet to keep order. Use LinkedHashSet if order matters.
  • Using the manual method for large arrays. It is O(n²) — prefer a Set.
  • Forgetting the import. Sets come from java.util.
  • Using primitives in a Set. Use Integer, not int.

Summary

  • Removing duplicates keeps one copy of each value.
  • A HashSet drops duplicates automatically — the easiest way.
  • A manual loop with contains also works but is slower.
  • Use LinkedHashSet to keep the original order.
  • The HashSet method is about O(n); the manual one is O(n²).

Next, learn sets in HashSet vs TreeSet, or work with arrays in Java arrays.

Frequently Asked Questions

How do I remove duplicates from an array in Java?
The easiest way is to add all elements to a HashSet, which automatically keeps only unique values. You can also do it manually by checking each element against the ones already kept.
Why is a HashSet good for removing duplicates?
A HashSet stores only unique items and rejects duplicates automatically. So adding every array element to a set leaves you with just the distinct values, with no extra checking code needed.
Does a HashSet keep the original order?
No, a plain HashSet does not keep insertion order. If you need the original order preserved, use a LinkedHashSet instead, which keeps items in the order they were added.
How do I remove duplicates without a Set?
Loop through the array and, for each element, check if it already appears in the results collected so far. If not, add it. This manual method works but is slower (O(n²)) than a HashSet.
What is the time complexity of the HashSet method?
It is roughly O(n), because adding to a HashSet is near constant time and you do it once per element. This is much faster than the manual O(n²) comparison approach for large arrays.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicFloyds Triangle Your progress: 60 / 61 (98%) · Lesson 61 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free