ThuyKhong преди 6 години
родител
ревизия
929fe2586d

+ 46
- 0
src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java Целия файл

@@ -1,8 +1,54 @@
1 1
 package com.zipcodewilmington.looplabs;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+import java.util.HashMap;
6
+
3 7
 /**
4 8
  * Created by leon on 1/29/18.
5 9
  * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
6 10
  */
7 11
 public final class IntegerDuplicateDeleter extends DuplicateDeleter<Integer> {
12
+
13
+
14
+    public IntegerDuplicateDeleter(Integer[] intArray) {
15
+        super(intArray);
16
+    }
17
+
18
+    //removes all values in the array which occur exactly the specified number of times.
19
+    @Override
20
+    public Integer[] removeDuplicates(int maxNumberOfDuplications) {
21
+        ArrayList<Integer> list = new ArrayList<>();
22
+        for(Integer i:array){
23
+            if(countDupes(i)<maxNumberOfDuplications){
24
+                list.add(i);
25
+            }
26
+        }
27
+        Integer[] result = new Integer[list.size()];
28
+        return list.toArray(result);
29
+    }
30
+
31
+    //removes all values in the array which occur at least the specified number of times.
32
+    @Override
33
+    public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
34
+        Integer[] result = new Integer[array.length];
35
+        int count = 0;
36
+        for (int i = 0; i < array.length; i++) {
37
+            if(countDupes(array[i])!=exactNumberOfDuplications){
38
+                result[count]=array[i];
39
+                count++;
40
+            }
41
+        }
42
+        return Arrays.copyOfRange(result,0,count);
43
+    }
44
+
45
+    public int countDupes(Integer integer){
46
+        int count = 0;
47
+        for(Integer i: array){
48
+            if(i==integer){
49
+                count++;
50
+            }
51
+        }
52
+        return count;
53
+    }
8 54
 }

+ 41
- 0
src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java Целия файл

@@ -1,8 +1,49 @@
1 1
 package com.zipcodewilmington.looplabs;
2 2
 
3
+import java.util.ArrayList;
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
+        ArrayList<String> list = new ArrayList<>();
18
+        for(String str:array){
19
+            if(countDuplicates(str)<maxNumberOfDuplications){
20
+                list.add(str);
21
+            }
22
+        }
23
+        String[] strArray = new String[list.size()];
24
+        strArray = list.toArray(strArray);
25
+        return strArray;
26
+    }
27
+
28
+    @Override
29
+    public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
30
+        ArrayList<String> list = new ArrayList<>();
31
+        for(String str:array){
32
+            if(countDuplicates(str)!=exactNumberOfDuplications){
33
+                list.add(str);
34
+            }
35
+        }
36
+        String[] strArray = new String[list.size()];
37
+        strArray = list.toArray(strArray);
38
+        return strArray;
39
+    }
40
+
41
+    public int countDuplicates(String string){
42
+        int count = 0;
43
+        for(String str:array){
44
+            if(str.equals(string))
45
+            count++;
46
+        }
47
+        return count;
48
+    }
8 49
 }