|
@@ -1,8 +1,130 @@
|
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.
|
|
8
|
+ * Given an object, `IntegerDuplicateDeleter`, with a composite `Integer[]` object, write a method
|
|
9
|
+ * * `removeDuplicatesExactly` which removes all values in the array which occur exactly
|
|
10
|
+ * the specified number of times.
|
|
11
|
+ * * `removeDuplicates` which removes all values in the array which occur
|
|
12
|
+ * at least the specified number of times.
|
6
|
13
|
*/
|
7
|
14
|
public final class IntegerDuplicateDeleter extends DuplicateDeleter<Integer> {
|
8
|
|
-}
|
|
15
|
+ ArrayList<Integer> list = new ArrayList<Integer>();
|
|
16
|
+
|
|
17
|
+ public IntegerDuplicateDeleter(Integer[] intArray) {
|
|
18
|
+ super(intArray);
|
|
19
|
+ }
|
|
20
|
+
|
|
21
|
+ @Override
|
|
22
|
+ public Integer[] removeDuplicates(int maxNumberOfDuplications) {
|
|
23
|
+ Arrays.sort (array);
|
|
24
|
+ int n = array.length;
|
|
25
|
+ int count = 0;
|
|
26
|
+ int num=0;
|
|
27
|
+ ArrayList<Integer>arraylist=new ArrayList<>() ;
|
|
28
|
+ //Get all unique values
|
|
29
|
+ HashSet set = new HashSet();
|
|
30
|
+ for (int i=0; i < array.length;i++) {
|
|
31
|
+ set.add(new Integer(array[i]));
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ Map<Integer,Integer>map=new HashMap<>();
|
|
35
|
+ Iterator it = set.iterator();
|
|
36
|
+ // Loop through the set for duplicates count and add it to Map
|
|
37
|
+ Integer temp;
|
|
38
|
+ while (it.hasNext()) {
|
|
39
|
+ //initialize the counter to 0
|
|
40
|
+ count = 0;
|
|
41
|
+ temp = (Integer) it.next();
|
|
42
|
+
|
|
43
|
+ //loop through the array sorted to count the duplicates
|
|
44
|
+
|
|
45
|
+ for (int j = 0; j < n; j++) {
|
|
46
|
+
|
|
47
|
+ if (temp.intValue() == array[j]) {
|
|
48
|
+
|
|
49
|
+ count++;
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ map.put(temp, count);
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ for(Map.Entry<Integer,Integer>entry: map.entrySet()){
|
|
60
|
+ if(entry.getValue().intValue() < maxNumberOfDuplications ){
|
|
61
|
+ for (int i=0; i< entry.getValue().intValue(); i++)
|
|
62
|
+ arraylist.add(entry.getKey() );
|
|
63
|
+
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ }
|
|
67
|
+ Integer[] integers =arraylist.toArray(new Integer[arraylist.size() ] ) ; //return new Integer[0];
|
|
68
|
+
|
|
69
|
+ return integers ;
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+ @Override
|
|
79
|
+ public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
|
|
80
|
+ Arrays.sort (array);
|
|
81
|
+ int n = array.length;
|
|
82
|
+ int count = 0;
|
|
83
|
+ int num=0;
|
|
84
|
+ ArrayList<Integer>arraylist=new ArrayList<>() ;
|
|
85
|
+ //Get all unique values
|
|
86
|
+ HashSet set = new HashSet();
|
|
87
|
+ for (int i=0; i < array.length;i++) {
|
|
88
|
+ set.add(new Integer(array[i]));
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ Map<Integer,Integer>map=new HashMap<>();
|
|
92
|
+ Iterator it = set.iterator();
|
|
93
|
+ // Loop through the set for duplicates count and add it to Map
|
|
94
|
+ Integer temp;
|
|
95
|
+ while (it.hasNext()) {
|
|
96
|
+ //initialize the counter to 0
|
|
97
|
+ count = 0;
|
|
98
|
+ temp = (Integer) it.next();
|
|
99
|
+
|
|
100
|
+ //loop through the array sorted to count the duplicates
|
|
101
|
+
|
|
102
|
+ for (int j = 0; j < n; j++) {
|
|
103
|
+
|
|
104
|
+ if (temp.intValue() == array[j]) {
|
|
105
|
+
|
|
106
|
+ count++;
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ map.put(temp, count);
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ for(Map.Entry<Integer,Integer>entry: map.entrySet()){
|
|
117
|
+ if(entry.getValue().intValue() != exactNumberOfDuplications){
|
|
118
|
+ for (int i=0; i< entry.getValue().intValue(); i++)
|
|
119
|
+ arraylist.add(entry.getKey() );
|
|
120
|
+
|
|
121
|
+ }
|
|
122
|
+
|
|
123
|
+ }
|
|
124
|
+ Integer[] integers =arraylist.toArray(new Integer[arraylist.size() ] ) ; //return new Integer[0];
|
|
125
|
+
|
|
126
|
+ return integers ;
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+}
|