NedRedmond 6 년 전
부모
커밋
f6db623a08

+ 72
- 0
src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java 파일 보기

@@ -1,8 +1,80 @@
1 1
 package com.zipcodewilmington.looplabs;
2 2
 
3
+import java.util.Arrays;
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
+    public IntegerDuplicateDeleter(Integer[] intArray) {
11
+        super(intArray);
12
+    }
13
+
14
+    private Integer[] tempArray;
15
+    private int arrCount;
16
+    private int dupCount;
17
+    private Integer dupToIgnore;
18
+
19
+    @Override
20
+    public Integer[] removeDuplicates(int maxNumberOfDuplications) {
21
+        Arrays.sort(array);
22
+        tempArray = new Integer[array.length];
23
+        arrCount = 0;
24
+        dupToIgnore = null;
25
+        for (int i = 0; i < array.length; i++) {
26
+            dupCount = 1;
27
+
28
+            if (array[i]==dupToIgnore) { continue; }
29
+
30
+            doopLoop(i);
31
+
32
+            writeToArray(i, dupCount < maxNumberOfDuplications);
33
+        }
34
+
35
+        return Arrays.copyOf(tempArray, arrCount);
36
+    }
37
+
38
+    private void doopLoop(int i) {
39
+        for (int j = i+1; j < array.length; j++) {
40
+            if (array[i] == array[j]) {
41
+                dupCount++;
42
+            } else {
43
+                break;
44
+            }
45
+        }
46
+    }
47
+
48
+    @Override
49
+    public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
50
+        Arrays.sort(array);
51
+        tempArray = new Integer[array.length];
52
+        arrCount = 0;
53
+        dupToIgnore = null;
54
+        for (int i = 0; i < array.length; i++) {
55
+            dupCount = 1;
56
+
57
+            if (array[i]==dupToIgnore) { continue; }
58
+            if (i>=1 && array[i]==array[i-1]) {
59
+                tempArray[arrCount] = array[i];
60
+                arrCount++;
61
+                continue;
62
+            }
63
+
64
+            doopLoop(i);
65
+
66
+            writeToArray(i, dupCount != exactNumberOfDuplications);
67
+        }
68
+
69
+        return Arrays.copyOf(tempArray, arrCount);
70
+    }
71
+
72
+    private void writeToArray(int i, boolean b) {
73
+        if (b) {
74
+            tempArray[arrCount] = array[i];
75
+            arrCount++;
76
+        } else {
77
+            dupToIgnore = array[i];
78
+        }
79
+    }
8 80
 }

+ 74
- 0
src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java 파일 보기

@@ -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
 }