Pages

How to Remove duplicates from array ?

Remove duplicates in array ?

There are different ways we can remove duplicates in array. By using Set, LinkedHashset, Streams etc.,. we can remove duplicates in array.

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class removeArrayDuplicates {

public static void main(String[] args) {
int a[] = { 10, 20, 30, 40, 50, 10, 20, 30 };

Set<Integer> s = new HashSet<>();

for (int arrayValue : a) {
s.add(arrayValue);
}
System.out.println("Final array with unique values using set=" + s);


System.out.println(
"<<----------------------------------------------->>");

Set<Integer> ns = new LinkedHashSet<>();

for (int arrayValue : a) {
ns.add(arrayValue);
}
System.out.println("Final array with unique values using Linked Hash Set=" + ns);


System.out.println(
"<<----------------------------------------------->>");

List<Integer> list = Arrays.stream(a).distinct().boxed().collect(Collectors.toList());
System.out.println("Final array with unique values using streams=" + list);


System.out.println(
"<<----------------------------------------------->>");
}
}
Output:
remove duplicates from array
remove duplicates from array in java
We can also remove duplicate from array using for loop as well.
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

public class removeArrayDuplicates {
public static void main(String[] args) {
int a[] = { 10, 20, 30, 40, 50, 10, 20, 30 };
System.out.println("<<----------------------------------------------->>");
Arrays.sort(a);
System.out.print("Sorted array=");
for (int y = 0; y < a.length; y++) {
System.out.print(a[y] + " ");
}
int[] temp =
new int[a.length];
int
k = 0;
for (int i = 0; i < a.length - 1; i++) {
if (a[i] != a[i + 1]) {
temp[
k] = a[i];
k++;
}
}
temp[
k] = a[a.length - 1];
System.
out.println();
System.
out.print("Final Unique values in array using for loop=");
for (int e = 0; e <= k; e++) {
System.
out.print(temp[e] + " ");
}
}
}
Output:
remove duplicates from array
We can also remove duplicate from array using HashMap keySet(),EntrySet().
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class removeArrayDuplicates {
public static void main(String[] args) {
int a[] = { 10, 10, 20, 30, 40, 50, 10, 20, 30 };
Map<Integer, Integer> hm = new HashMap<Integer, Integer>();
for (int n : a) {
if (!hm.containsKey(n)) {
hm.put(n, 1);
}
else {
hm.
put(n, hm.get(n) + 1);
}
}
System.out.println(
"Hash map=" + hm);
System.out.println(
"<<----------------------------------------------->>");
Set<Entry<Integer, Integer>> entries = hm.entrySet();
System.out.print("Final duplicate values using Hash map entry Set=");
for (Entry<Integer, Integer> entry : entries) {
if (entry.getValue() > 1) {
System.out.print(entry.getKey() + " ");
}
}
System.out.println();
System.out.print("Final unique values using Hash map entry Set=");
for (Entry<Integer, Integer> entry : entries) {
if (entry.getValue() <= 1) {
System.out.print(entry.getKey() + " ");
}
}

System.out.println();
System.out.println(
"<<----------------------------------------------->>");
System.out.println("Hash map=" + hm);
Set<Integer> hashKeys = hm.keySet();
System.out.print("Final duplicate values using Hash map keySet()=");
for (int key : hashKeys) {
if (hm.get(key) > 1) {
System.out.print(key + " ");
}
}
System.out.println();
System.out.print("Final unique values using Hash map keySet()=");
for (int key : hashKeys) {
if (hm.get(key) <= 1) {
System.out.print(key + " ");
}
}
}

}
Output:
remove duplicates using hash map

Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.