-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveDuplicates.java
50 lines (44 loc) · 1.35 KB
/
RemoveDuplicates.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.HashSet;
public class RemoveDuplicates {
Node root;
public static class Node {
int data;
Node next;
Node(int value) {
data = value;
next = null;
}
}
public static void createLists(RemoveDuplicates list1, int[] ar1) {
Node currNode1 = list1.root;
for (int i = 0; i < ar1.length; i++) {
Node newNode = new Node(ar1[i]);
if (list1.root == null) {
list1.root = newNode;
currNode1 = newNode;
} else {
currNode1.next = newNode;
currNode1 = newNode;
}
}
removedDuplicates(list1);
}
public static void removedDuplicates(RemoveDuplicates list1) {
HashSet<Integer> originalData = new HashSet<>();
Node currentNode = list1.root;
while (currentNode != null) {
if (!originalData.contains(currentNode.data)) {
originalData.add(currentNode.data);
}
currentNode = currentNode.next;
}
for (int data : originalData) {
System.out.print(data + "\t");
}
}
public static void main(String[] args) {
RemoveDuplicates list1 = new RemoveDuplicates();
int[] ar1 = {1, 8, 4, 4, 5};
createLists(list1, ar1);
}
}