-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicate.java
More file actions
93 lines (82 loc) · 3.27 KB
/
RemoveDuplicate.java
File metadata and controls
93 lines (82 loc) · 3.27 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
/**
* Created by ignacioojanguren on 31/10/16.
* Remove the duplicate values in an array already sorted.
* The difficulty of this program is to use the Big O notation to be O(n) and try not to
* do a O(n^2).
* I will do both of the notations. The O(n) will perform faster than O(n^2), so it is recomendable
* to use O(n).
*
*/
public class RemoveDuplicate {
/**
* Function using O(n) notation.
*
* Created a Generic Method that will allow to check if there is any duplicates in the array.
* This method creates a HashSet that will store the non repeated values.
*
* @param elements
* This is an array T indicates it can be any type of value, like String, Integer, Double, ...
* @return
* Returns the HashSet with the values non repeated
*/
public static <T> HashSet<T> removeDuplicates(T[] elements){
HashSet<T> withoutDuplicates = new HashSet<T>();
for(T element: elements){
if(!withoutDuplicates.contains(element)){
withoutDuplicates.add(element);
}
}
return withoutDuplicates;
}
/**
* Function using O(n^2) notation.
*
* This method returns the array with the elements removed. However, it appears a null where the duplicate element was.
* The array has an static dimension, making not possible to remove the position completely.
* It is a generic method, which will admit any value for the array "elements"
*
* @param elements
* Element is a generic array that contains the duplicate elements.
* @return
* return the array elements with the duplicates removed
*/
public static <T> T[] removeDuplicatesSlow(T[] elements){
for (int i = 0; i < elements.length; i++){
for (int j = 0; j < elements.length; j++){
if( i != j && elements[i].equals(elements[j])){
elements[i] = null;
break;
}
}
}
return elements;
}
public static void main(String[] args){
//Testing with Strings
HashSet<String> noDuplicateList = new HashSet<String>();
String[] sortedList = {"Airplane", "Bicycle", "Bicycle", "Boat", "Car", "Car","Train"};
noDuplicateList = removeDuplicates(sortedList);
for(String element: noDuplicateList){
System.out.print(element + " ");
}
System.out.println("\n -- ");
//Testing with Integers
HashSet<Integer> noDuplicateListInt = new HashSet<Integer>();
noDuplicateListInt = new HashSet<Integer>();
Integer[] sortedListInt = {1, 1, 1, 2, 3, 4, 5, 6, 7, 7};
noDuplicateListInt = removeDuplicates(sortedListInt);
for(Integer element: noDuplicateListInt){
System.out.print(element + " ");
}
System.out.println("\n -- ");
sortedList = new String[]{"Airplane", "Bicycle", "Bicycle", "Boat", "Car", "Car","Train"};
sortedList = removeDuplicatesSlow(sortedList);
for(String elements: sortedList){
// Do not show if the value of the value of element[i] is null.
if(elements!= null)System.out.print(elements + " ");
}
}
}