📘 Lesson · Lesson 59
Remove Duplicates
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
Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें.
Agar load na ho to naye tab mein kholें.