Browse Source

Finished, thanks Dave

Kris Blassingame 6 years ago
parent
commit
e5e0c4cfeb

+ 45
- 2
IntegerDuplicateDeleter.java View File

@@ -1,6 +1,49 @@
1
- 
2
-
1
+import java.util.*;
3 2
 
4 3
 public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
4
+    
5
+    public IntegerDuplicateDeleter(Integer[] array){
6
+        super(array);
7
+    } 
8
+
9
+    public Integer[] removeDuplicates(int maxNumberOfDuplications){
10
+        Integer[] answer = new Integer[array.length];
11
+        int answerIndex = 0;
12
+        for (int i = 0; i < array.length; i++){
13
+            Integer occurences = getAppearances(array[i]);
14
+            if (occurences < maxNumberOfDuplications) {
15
+                answer[answerIndex] = array[i];
16
+                answerIndex++;
17
+            }
18
+        }
19
+        Integer[] realAnswer = Arrays.copyOf(answer, answerIndex);
20
+        return realAnswer;
21
+    }
22
+
23
+    public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications){
24
+        Integer[] answer = new Integer[array.length];
25
+        int answerIndex = 0;
26
+        for (int i = 0; i < array.length; i++){
27
+            Integer occurences = getAppearances(array[i]);
28
+            if (occurences < exactNumberOfDuplications || occurences > exactNumberOfDuplications) {
29
+                answer[answerIndex] = array[i];
30
+                answerIndex++;
31
+            }
32
+        }
33
+        Integer[] realAnswer = Arrays.copyOf(answer, answerIndex);
34
+        return realAnswer;
35
+
36
+    }
37
+
38
+    public int getAppearances(Integer value) {
39
+        int appearances = 0;
40
+
41
+        for (Integer element : array){
42
+            if(element == value){
43
+                appearances++;
44
+            }
45
+        }
46
+        return appearances;
47
+    }
5 48
 
6 49
 }

+ 0
- 11
IntegerDuplicateDeleterTest.java View File

@@ -1,4 +1,3 @@
1
- 
2 1
 
3 2
 import org.junit.Assert;
4 3
 import org.junit.Before;
@@ -62,7 +61,6 @@ public class IntegerDuplicateDeleterTest {
62 61
         Assert.assertArrayEquals(expected, actual);
63 62
     }
64 63
 
65
-
66 64
     @Test
67 65
     public void testRemoveDuplicatesExactly_withOccuranceInTheMiddle() {
68 66
         Integer[] intArray = new Integer[]{0, 0, 0, 1, 2, 2, 4, 4, 5, 5, 5, 6, 9, 9, 9};
@@ -77,11 +75,6 @@ public class IntegerDuplicateDeleterTest {
77 75
 
78 76
 
79 77
 
80
-
81
-
82
-
83
-
84
-
85 78
     @Test
86 79
     public void testRemoveDuplicates_whenThereIsOnlyOneElement() {
87 80
         Integer[] intArray = new Integer[]{5};
@@ -140,7 +133,6 @@ public class IntegerDuplicateDeleterTest {
140 133
         Assert.assertArrayEquals(expected, actual);
141 134
     }
142 135
 
143
-
144 136
     @Test
145 137
     public void testRemoveDuplicates_withOccuranceAtTheBeginningAndEnd() {
146 138
         Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
@@ -152,8 +144,6 @@ public class IntegerDuplicateDeleterTest {
152 144
 
153 145
 
154 146
 
155
-
156
-
157 147
     @Test
158 148
     public void testRemoveDuplicatesExactlyIdempotence() {
159 149
         Integer[] input = {11, 3, 3, 3, 4, 4, 4, 4};
@@ -170,7 +160,6 @@ public class IntegerDuplicateDeleterTest {
170 160
         Assert.assertArrayEquals(expected, actual3);
171 161
     }
172 162
 
173
-
174 163
     @Test
175 164
     public void testRemoveDuplicatesIdempotence() {
176 165
         Integer[] input = {1, 2, 2, 2, 3, 4, 4};

+ 43
- 2
StringDuplicateDeleter.java View File

@@ -1,6 +1,47 @@
1
- 
2
-
1
+import java.util.*;
3 2
 public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
4 3
 
4
+    public StringDuplicateDeleter(String[] array){
5
+        super(array);
6
+    } 
7
+
8
+    public String[] removeDuplicates(int maxNumberOfDuplications){
9
+        String[] answer = new String[array.length];
10
+        int answerIndex = 0;
11
+        for (int i = 0; i < array.length; i++){
12
+            Integer occurences = getAppearances(array[i]);
13
+            if (occurences < maxNumberOfDuplications) {
14
+                answer[answerIndex] = array[i];
15
+                answerIndex++;
16
+            }
17
+        }
18
+        String[] realAnswer = Arrays.copyOf(answer, answerIndex);
19
+        return realAnswer;
20
+    }
21
+
22
+    public String[] removeDuplicatesExactly(int exactNumberOfDuplications){
23
+        String[] answer = new String[array.length];
24
+        int answerIndex = 0;
25
+        for (int i = 0; i < array.length; i++){
26
+            Integer occurences = getAppearances(array[i]);
27
+            if (occurences < exactNumberOfDuplications || occurences > exactNumberOfDuplications) {
28
+                answer[answerIndex] = array[i];
29
+                answerIndex++;
30
+            }
31
+        }
32
+        String[] realAnswer = Arrays.copyOf(answer, answerIndex);
33
+        return realAnswer;
34
+
35
+    }
36
+
37
+    public int getAppearances(String value) {
38
+        int appearances = 0;
5 39
 
40
+        for (String element : array){
41
+            if(element.equals(value)){
42
+                appearances++;
43
+            }
44
+        }
45
+        return appearances;
46
+    }
6 47
 }

+ 0
- 11
StringDuplicateDeleterTest.java View File

@@ -1,4 +1,3 @@
1
- 
2 1
 
3 2
 
4 3
 import org.junit.Assert;
@@ -62,7 +61,6 @@ public class StringDuplicateDeleterTest {
62 61
         Assert.assertArrayEquals(expected, actual);
63 62
     }
64 63
 
65
-
66 64
     @Test
67 65
     public void testRemoveDuplicatesExactly_withOccuranceInTheMiddle() {
68 66
         String[] array = new String[]{"cat", "cat", "cat", "deer", "deer", "dog", "fox", "fox", "goat", "goat", "goat"};
@@ -77,11 +75,6 @@ public class StringDuplicateDeleterTest {
77 75
 
78 76
 
79 77
 
80
-
81
-
82
-
83
-
84
-
85 78
     @Test
86 79
     public void testRemoveDuplicates_withOneElement() {
87 80
         String[] array = new String[]{"Droolius Caesar"};
@@ -131,7 +124,6 @@ public class StringDuplicateDeleterTest {
131 124
         Assert.assertArrayEquals(expected, actual);
132 125
     }
133 126
 
134
-
135 127
     @Test
136 128
     public void testRemoveDuplicates_withOccuranceAtTheBeginningAndEnd() {
137 129
         String[] array = new String[]{"aba", "aba", "aba", "bab", "aaa", "bba", "bba", "bbb", "bbb", "bbb"};
@@ -143,8 +135,6 @@ public class StringDuplicateDeleterTest {
143 135
 
144 136
 
145 137
 
146
-
147
-
148 138
     @Test
149 139
     public void testRemoveDuplicatesExactlyIdempotence() {
150 140
         String[] input = {"aa", "b", "b", "b", "c", "c", "t", "t", "t"};
@@ -161,7 +151,6 @@ public class StringDuplicateDeleterTest {
161 151
         Assert.assertArrayEquals(expected, actual3);
162 152
     }
163 153
 
164
-
165 154
     @Test
166 155
     public void testRemoveDuplicatesIdempotence() {
167 156
         String[] input = {"aa", "b", "b", "b", "c", "c", "t", "t"};