Jennifer Chao il y a 6 ans
Parent
révision
6e7bff5083

+ 53
- 0
src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java Voir le fichier

@@ -1,8 +1,61 @@
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
+
11
+    public IntegerDuplicateDeleter(Integer[] intArray) {
12
+        super(intArray);
13
+    }
14
+
15
+    @Override
16
+    public Integer[] removeDuplicates(int maxNumberOfDuplications) {
17
+        Integer[] dupeCounts = countDupes();
18
+        Integer[] noXDupes = new Integer[this.array.length];
19
+        int index = 0;
20
+
21
+        for (int i = 0; i < dupeCounts.length; i++) {
22
+            if (dupeCounts[i] < maxNumberOfDuplications) {
23
+                noXDupes[index] = array[i];
24
+                index++;
25
+            }
26
+        }
27
+
28
+        return Arrays.copyOfRange(noXDupes, 0, index);
29
+    }
30
+
31
+    @Override
32
+    public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
33
+        Integer[] dupeCounts = countDupes();
34
+        Integer[] noXDupes = new Integer[this.array.length];
35
+        int index = 0;
36
+
37
+        for (int i = 0; i < dupeCounts.length; i++) {
38
+            if (dupeCounts[i] != exactNumberOfDuplications) {
39
+                noXDupes[index] = array[i];
40
+                index++;
41
+            }
42
+        }
43
+
44
+        return Arrays.copyOfRange(noXDupes, 0, index);
45
+    }
46
+
47
+    public Integer[] countDupes() {
48
+        Integer[] dupeCount = new Integer[this.array.length];
49
+        Arrays.fill(dupeCount, 0);
50
+
51
+        for (int i = 0; i < array.length; i++) {
52
+            for (int j = 0; j < array.length ; j++) {
53
+                if (array[i].equals(array[j])) {
54
+                    dupeCount[i]++;
55
+                }
56
+            }
57
+        }
58
+
59
+        return dupeCount;
60
+    }
8 61
 }

+ 53
- 0
src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java Voir le fichier

@@ -1,8 +1,61 @@
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
+
11
+    public StringDuplicateDeleter(String[] intArray) {
12
+        super(intArray);
13
+    }
14
+
15
+    @Override
16
+    public String[] removeDuplicates(int maxNumberOfDuplications) {
17
+        Integer[] dupeCounts = countDupes();
18
+        String[] noXDupes = new String[this.array.length];
19
+        int index = 0;
20
+
21
+        for (int i = 0; i < dupeCounts.length; i++) {
22
+            if (dupeCounts[i] < maxNumberOfDuplications) {
23
+                noXDupes[index] = array[i];
24
+                index++;
25
+            }
26
+        }
27
+
28
+        return Arrays.copyOfRange(noXDupes, 0, index);
29
+    }
30
+
31
+    @Override
32
+    public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
33
+        Integer[] dupeCounts = countDupes();
34
+        String[] noXDupes = new String[this.array.length];
35
+        int index = 0;
36
+
37
+        for (int i = 0; i < dupeCounts.length; i++) {
38
+            if (dupeCounts[i] != exactNumberOfDuplications) {
39
+                noXDupes[index] = array[i];
40
+                index++;
41
+            }
42
+        }
43
+
44
+        return Arrays.copyOfRange(noXDupes, 0, index);
45
+    }
46
+
47
+    public Integer[] countDupes() {
48
+        Integer[] dupeCount = new Integer[array.length];
49
+        Arrays.fill(dupeCount, 0);
50
+
51
+        for (int i = 0; i < array.length; i++) {
52
+            for (int j = 0; j < array.length; j++) {
53
+                if (array[i].equals(array[j])) {
54
+                    dupeCount[i]++;
55
+                }
56
+            }
57
+        }
58
+
59
+        return dupeCount;
60
+    }
8 61
 }

+ 13
- 0
src/test/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleterTest.java Voir le fichier

@@ -1,5 +1,6 @@
1 1
 package com.zipcodewilmington.looplabs;
2 2
 
3
+import org.junit.Assert;
3 4
 import org.junit.Before;
4 5
 import org.junit.Test;
5 6
 
@@ -22,6 +23,16 @@ public class IntegerDuplicateDeleterTest {
22 23
 
23 24
 
24 25
     @Test
26
+    public void testCountDupes(){
27
+        IntegerDuplicateDeleter integerDuplicateDeleter = new IntegerDuplicateDeleter(intArray);
28
+
29
+        Integer[] expected = new Integer[] {3, 3, 3, 1, 2, 2, 2, 2, 3, 3, 3, 1, 3, 3, 3};
30
+        Integer[] actual = integerDuplicateDeleter.countDupes();
31
+
32
+        Assert.assertArrayEquals(expected, actual);
33
+    }
34
+
35
+    @Test
25 36
     public void testRemoveDuplicatesExactlyExactly() {
26 37
         Integer[] expected = new Integer[]{1, 2, 2, 4, 4, 6};
27 38
         Integer[] actual = deleter.removeDuplicatesExactly(3);
@@ -90,6 +101,8 @@ public class IntegerDuplicateDeleterTest {
90 101
         Integer[] expected = new Integer[]{};
91 102
         Integer[] actual = deleter.removeDuplicates(0);
92 103
         TestUtils.assertArrayEquality(expected, actual);
104
+
105
+        ((IntegerDuplicateDeleter) deleter).countDupes();
93 106
     }
94 107
 
95 108