Repository navigation
Expand file tree
/
Copy pathRemoveDUP.java
More file actions
57 lines (46 loc) · 1.53 KB
/
Copy pathRemoveDUP.java
File metadata and controls
57 lines (46 loc) · 1.53 KB
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
51
52
53
54
55
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
public class RemoveDUP {
// Remove duplicates from a sorted/unsorted linked list.
/* public static void main(String[] args) {
int number[] = {1, 2, 3, 4, 5, 1, 2, 3, 4, 6};
int[] result = remove(number);
System.out.println(Arrays.toString(result));
}
public static int[] remove(int[] number) {
if (number.length == 0) {
return new int[0];
}
Map<Integer, Integer> hash = new HashMap<>();
int count = 0;
for (int num : number) {
if (!hash.containsKey(num)) {
number[count++] = num;
hash.put(num, 1);
}
}
return Arrays.copyOf(number, count);
} */
public static void main(String[] args) {
LinkedList<Integer> linkedList = new LinkedList<>();
int[] array = {1,1, 2, 3, 4, 5};
for (int value : array) {
linkedList.add(value);
}
LinkedList<Integer> result = removeDuplicates(linkedList);
System.out.println("Result: " + result);
}
public static LinkedList<Integer> removeDuplicates(LinkedList<Integer> numbers) {
Map<Integer, Integer> hash = new HashMap<>();
LinkedList<Integer> result = new LinkedList<>();
for (int num : numbers) {
if (!hash.containsKey(num)) {
result.add(num);
hash.put(num, 1);
}
}
return result;
}
}