📘 Lesson · Lesson 59
Remove Duplicates
Remove Duplicate Elements from an Array in Java
This removes duplicate elements from an array using a LinkedHashSet (keeps order, removes repeats).
Java Program
int[] a = {1, 2, 2, 3, 4, 4, 5};
java.util.Set<Integer> set = new java.util.LinkedHashSet<>();
for (int x : a) set.add(x);
System.out.println(set); // [1, 2, 3, 4, 5]
Output
[1, 2, 3, 4, 5]
How it Works
- A Set automatically rejects duplicate values.
- LinkedHashSet keeps the original insertion order.
Summary
- This removes duplicate elements from an array using a LinkedHashSet (keeps order, removes repeats).
Remove Duplicate Elements from an Array in Java
यह LinkedHashSet से array से duplicate elements हटाता है (order रखता है, repeats हटाता है)।
Java Program
int[] a = {1, 2, 2, 3, 4, 4, 5};
java.util.Set<Integer> set = new java.util.LinkedHashSet<>();
for (int x : a) set.add(x);
System.out.println(set); // [1, 2, 3, 4, 5]
Output
[1, 2, 3, 4, 5]
कैसे काम करता है
- Set अपने आप duplicate values reject करता है।
- LinkedHashSet original insertion order रखता है।
सारांश
- यह LinkedHashSet से array से duplicate elements हटाता है (order रखता है, repeats हटाता है)।
💻 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.