|
@@ -1,8 +1,82 @@
|
1
|
1
|
package com.zipcodewilmington.looplabs;
|
2
|
2
|
|
|
3
|
+import java.util.Arrays;
|
|
4
|
+
|
3
|
5
|
/**
|
4
|
6
|
* Created by leon on 1/28/18.
|
5
|
7
|
* @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
|
6
|
8
|
*/
|
7
|
9
|
public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
|
|
10
|
+ public StringDuplicateDeleter(String[] intArray) {
|
|
11
|
+ super(intArray);
|
|
12
|
+ }
|
|
13
|
+
|
|
14
|
+ private String[] tempArray;
|
|
15
|
+ private int arrCount;
|
|
16
|
+ private int dupCount;
|
|
17
|
+ private String dupToIgnore;
|
|
18
|
+
|
|
19
|
+ @Override
|
|
20
|
+ public String[] removeDuplicates(int maxNumberOfDuplications) {
|
|
21
|
+ Arrays.sort(array);
|
|
22
|
+ tempArray = new String[array.length];
|
|
23
|
+ arrCount = 0;
|
|
24
|
+ dupToIgnore = null;
|
|
25
|
+ for (int i = 0; i < array.length; i++) {
|
|
26
|
+ dupCount = 1;
|
|
27
|
+
|
|
28
|
+ // IGNORE AFTER FIRST OCCURRENCE
|
|
29
|
+ if (array[i].equals(dupToIgnore)) { continue; }
|
|
30
|
+
|
|
31
|
+ doopLoop(i);
|
|
32
|
+
|
|
33
|
+ writeToArray(i, dupCount < maxNumberOfDuplications);
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ return Arrays.copyOf(tempArray, arrCount);
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ private void doopLoop(int i) {
|
|
40
|
+ for (int j = i+1; j < array.length; j++) {
|
|
41
|
+ if (array[i].equals(array[j])) {
|
|
42
|
+ dupCount++;
|
|
43
|
+ } else {
|
|
44
|
+ break;
|
|
45
|
+ }
|
|
46
|
+ }
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ @Override
|
|
50
|
+ public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
|
|
51
|
+ Arrays.sort(array);
|
|
52
|
+ tempArray = new String[array.length];
|
|
53
|
+ arrCount = 0;
|
|
54
|
+ dupToIgnore = null;
|
|
55
|
+ for (int i = 0; i < array.length; i++) {
|
|
56
|
+ dupCount = 1;
|
|
57
|
+
|
|
58
|
+ // IGNORE OR INCLUDE AFTER FIRST OCCURRENCE
|
|
59
|
+ if (array[i].equals(dupToIgnore)) { continue; }
|
|
60
|
+ if (i>0 && array[i].equals(array[i - 1])) {
|
|
61
|
+ tempArray[arrCount] = array[i];
|
|
62
|
+ arrCount++;
|
|
63
|
+ continue;
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ doopLoop(i);
|
|
67
|
+
|
|
68
|
+ writeToArray(i, dupCount != exactNumberOfDuplications);
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ return Arrays.copyOf(tempArray, arrCount);
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ private void writeToArray(int i, boolean b) {
|
|
75
|
+ if (b) {
|
|
76
|
+ tempArray[arrCount] = array[i];
|
|
77
|
+ arrCount++;
|
|
78
|
+ } else {
|
|
79
|
+ dupToIgnore = array[i];
|
|
80
|
+ }
|
|
81
|
+ }
|
8
|
82
|
}
|