Demetrius Murray 6 年之前
父節點
當前提交
dc5514a436

+ 58
- 0
src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java 查看文件

@@ -1,8 +1,66 @@
1 1
 package com.zipcodewilmington.looplabs;
2 2
 
3
+import java.util.Arrays;
4
+import java.util.stream.Stream;
5
+
3 6
 /**
4 7
  * Created by leon on 1/29/18.
5 8
  * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
6 9
  */
7 10
 public final class IntegerDuplicateDeleter extends DuplicateDeleter<Integer> {
11
+
12
+    public IntegerDuplicateDeleter(Integer[] intArray) {
13
+        super(intArray);
14
+    }
15
+
16
+    @Override //note: README says to remove values that occur AT LEAST. H/e parameter implies AT MOST.
17
+    public Integer[] removeDuplicates(int maxNumberOfDuplications) {
18
+        Integer[] array2 = Arrays.copyOf(array, array.length);
19
+        StringBuilder sb = new StringBuilder();
20
+        for (int i = 0; i < array2.length; i++) {
21
+            int count = 1;
22
+            for (int j = 0; j < array2.length; j++) {
23
+                if (array2[j].equals(array2[i])) {
24
+                    count++;
25
+                }
26
+            }
27
+            if (count <= maxNumberOfDuplications)
28
+                sb.append(array2[i]).append("/");
29
+        }
30
+        if (!sb.toString().equals("")) {
31
+            array2 = getNewArray(sb);
32
+        }
33
+        else { array2 = new Integer[0];}
34
+        return array2;
35
+    }
36
+
37
+    @Override
38
+    public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
39
+        StringBuilder sb = new StringBuilder();
40
+        Integer[] array2 = Arrays.copyOf(array, array.length);
41
+        for (int i = 0; i < array2.length; i++) {
42
+            int count = 0;
43
+            for (int j = 0; j < array2.length; j++) {
44
+                if (array2[j].equals(array2[i])) {
45
+                    count++;
46
+                }
47
+            }
48
+            if (count != exactNumberOfDuplications)
49
+                sb.append(array2[i]).append("/");
50
+        }
51
+        if (!sb.toString().equals("")) {
52
+            array2 = getNewArray(sb);
53
+        }
54
+        else { array2 = new Integer[0];}
55
+        return array2;
56
+    }
57
+
58
+    private Integer[] getNewArray(StringBuilder sb) {
59
+        Integer[] array2;
60
+        String[] s = sb.toString().split("/");
61
+        array2 = new Integer[s.length];
62
+        for (int i = 0; i < array2.length; i++)
63
+            array2[i] = Integer.valueOf(s[i]);
64
+        return array2;
65
+    }
8 66
 }

+ 58
- 0
src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java 查看文件

@@ -1,8 +1,66 @@
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
+    @Override
15
+    public String[] removeDuplicates(int maxNumberOfDuplications) {
16
+        String[] array2 = Arrays.copyOf(array, array.length);
17
+        StringBuilder sb = new StringBuilder();
18
+        for (String str1 : array2) {
19
+            int count = 1;
20
+
21
+            for (String str2 : array2)
22
+                if (str2.equals(str1))
23
+                    count++;
24
+
25
+            if (count <= maxNumberOfDuplications)
26
+                sb.append(str1).append("/");
27
+        }
28
+
29
+        if (!sb.toString().equals(""))
30
+            array2 = getNewArray(sb);
31
+        else array2 = new String[0];
32
+
33
+        return array2;
34
+    }
35
+
36
+    @Override
37
+    public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
38
+        StringBuilder sb = new StringBuilder();
39
+        String[] array2 = Arrays.copyOf(array, array.length);
40
+        for (String str1 : array2) {
41
+            int count = 0;
42
+
43
+            for (String str2 : array2)
44
+                if (str2.equals(str1))
45
+                    count++;
46
+
47
+            if (count != exactNumberOfDuplications)
48
+                sb.append(str1).append("/");
49
+        }
50
+
51
+        if (!sb.toString().equals(""))
52
+            array2 = getNewArray(sb);
53
+        else array2 = new String[0];
54
+
55
+        return array2;
56
+    }
57
+
58
+    private String[] getNewArray(StringBuilder sb) {
59
+        String[] array2;
60
+        String[] s = sb.toString().split("/");
61
+        array2 = new String[s.length];
62
+        for (int i = 0; i < array2.length; i++)
63
+            array2[i] = s[i];
64
+        return array2;
65
+    }
8 66
 }