|
@@ -1,8 +1,121 @@
|
1
|
1
|
package com.zipcodewilmington.looplabs;
|
2
|
2
|
|
|
3
|
+import java.util.*;
|
|
4
|
+
|
3
|
5
|
/**
|
4
|
6
|
* Created by leon on 1/29/18.
|
5
|
7
|
* @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
|
6
|
8
|
*/
|
7
|
9
|
public final class IntegerDuplicateDeleter extends DuplicateDeleter<Integer> {
|
|
10
|
+ ArrayList<Integer> list = new ArrayList<Integer>();
|
|
11
|
+
|
|
12
|
+ public IntegerDuplicateDeleter(Integer[] intArray) {
|
|
13
|
+ super(intArray);
|
|
14
|
+ }
|
|
15
|
+
|
|
16
|
+ @Override
|
|
17
|
+ public Integer[] removeDuplicates(int maxNumberOfDuplications) {
|
|
18
|
+ Arrays.sort (array);
|
|
19
|
+ int n = array.length;
|
|
20
|
+ int count = 0;
|
|
21
|
+ int num=0;
|
|
22
|
+ ArrayList<Integer>arraylist=new ArrayList<>() ;
|
|
23
|
+ //Get all unique values
|
|
24
|
+ HashSet set = new HashSet();
|
|
25
|
+ for (int i=0; i < array.length;i++) {
|
|
26
|
+ set.add(new Integer(array[i]));
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+ Map<Integer,Integer> map=new HashMap<>();
|
|
30
|
+ Iterator it = set.iterator();
|
|
31
|
+ // Loop through the set for duplicates count and add it to Map
|
|
32
|
+ Integer temp;
|
|
33
|
+ while (it.hasNext()) {
|
|
34
|
+ //initialize the counter to 0
|
|
35
|
+ count = 0;
|
|
36
|
+ temp = (Integer) it.next();
|
|
37
|
+
|
|
38
|
+ //loop through the array sorted to count the duplicates
|
|
39
|
+
|
|
40
|
+ for (int j = 0; j < n; j++) {
|
|
41
|
+
|
|
42
|
+ if (temp.intValue() == array[j]) {
|
|
43
|
+
|
|
44
|
+ count++;
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ map.put(temp, count);
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ for(Map.Entry<Integer,Integer>entry: map.entrySet()){
|
|
55
|
+ if(entry.getValue().intValue() < maxNumberOfDuplications ){
|
|
56
|
+ for (int i=0; i< entry.getValue().intValue(); i++)
|
|
57
|
+ arraylist.add(entry.getKey() );
|
|
58
|
+
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ }
|
|
62
|
+ Integer[] integers =arraylist.toArray(new Integer[arraylist.size() ] ) ; //return new Integer[0];
|
|
63
|
+
|
|
64
|
+ return integers ;
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ @Override
|
|
70
|
+ public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
|
|
71
|
+ Arrays.sort (array);
|
|
72
|
+ int n = array.length;
|
|
73
|
+ int count = 0;
|
|
74
|
+ int num=0;
|
|
75
|
+ ArrayList<Integer>arraylist=new ArrayList<>() ;
|
|
76
|
+ //Get all unique values
|
|
77
|
+ HashSet set = new HashSet();
|
|
78
|
+ for (int i=0; i < array.length;i++) {
|
|
79
|
+ set.add(new Integer(array[i]));
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ Map<Integer,Integer>map=new HashMap<>();
|
|
83
|
+ Iterator it = set.iterator();
|
|
84
|
+ // Loop through the set for duplicates count and add it to Map
|
|
85
|
+ Integer temp;
|
|
86
|
+ while (it.hasNext()) {
|
|
87
|
+ //initialize the counter to 0
|
|
88
|
+ count = 0;
|
|
89
|
+ temp = (Integer) it.next();
|
|
90
|
+
|
|
91
|
+ //loop through the array sorted to count the duplicates
|
|
92
|
+
|
|
93
|
+ for (int j = 0; j < n; j++) {
|
|
94
|
+
|
|
95
|
+ if (temp.intValue() == array[j]) {
|
|
96
|
+
|
|
97
|
+ count++;
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ map.put(temp, count);
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ for(Map.Entry<Integer,Integer>entry: map.entrySet()){
|
|
108
|
+ if(entry.getValue().intValue() != exactNumberOfDuplications){
|
|
109
|
+ for (int i=0; i< entry.getValue().intValue(); i++)
|
|
110
|
+ arraylist.add(entry.getKey() );
|
|
111
|
+
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ }
|
|
115
|
+ Integer[] integers =arraylist.toArray(new Integer[arraylist.size() ] ) ; //return new Integer[0];
|
|
116
|
+
|
|
117
|
+ return integers ;
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+
|
8
|
121
|
}
|