Brandon Defrancis 6 년 전
부모
커밋
f5fde4b4c8

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

1
 package com.zipcodewilmington.looplabs;
1
 package com.zipcodewilmington.looplabs;
2
 
2
 
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+
3
 /**
6
 /**
4
  * Created by leon on 1/29/18.
7
  * Created by leon on 1/29/18.
5
  * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
8
  * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
6
  */
9
  */
7
 public final class IntegerDuplicateDeleter extends DuplicateDeleter<Integer> {
10
 public final class IntegerDuplicateDeleter extends DuplicateDeleter<Integer> {
11
+
12
+    public IntegerDuplicateDeleter(Integer[] intArray) {
13
+        super(intArray);
14
+    }
15
+
16
+    @Override
17
+    public Integer[] removeDuplicates(int maxNumberOfDuplications) {
18
+        Integer[] intArr = new Integer[array.length];
19
+        int c = 0;
20
+        for (Integer anArray : array) {
21
+            if (count(anArray) < maxNumberOfDuplications) {
22
+                intArr[c] = anArray;
23
+                c++;
24
+            }
25
+        }
26
+        return Arrays.copyOfRange(intArr,0, c);
27
+    }
28
+
29
+    @Override
30
+    public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
31
+        Integer[] intArr = new Integer[array.length];
32
+        int c = 0;
33
+        for (Integer anArray : array) {
34
+            if (count(anArray) != exactNumberOfDuplications) {
35
+                intArr[c] = anArray;
36
+                c++;
37
+            }
38
+        }
39
+        return Arrays.copyOfRange(intArr,0, c);
40
+    }
41
+
42
+    private int count(int arrNum){
43
+        int c = 0;
44
+        for (int index : array){
45
+            if(index == arrNum){
46
+                c++;
47
+            }
48
+        }
49
+        return c;
50
+    }
51
+
8
 }
52
 }

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

1
 package com.zipcodewilmington.looplabs;
1
 package com.zipcodewilmington.looplabs;
2
 
2
 
3
+import java.util.Arrays;
4
+
3
 /**
5
 /**
4
  * Created by leon on 1/28/18.
6
  * Created by leon on 1/28/18.
5
  * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
7
  * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
6
  */
8
  */
7
 public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
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
+        String[] stringArr = new String[array.length];
18
+        int c = 0;
19
+        for (String anArray : array) {
20
+            if (countDupes(anArray) < maxNumberOfDuplications) {
21
+                stringArr[c] = anArray;
22
+                c++;
23
+            }
24
+        }
25
+        return Arrays.copyOfRange(stringArr,0, c);
26
+    }
27
+
28
+
29
+    @Override
30
+    public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
31
+
32
+        String[] stringArr = new String[array.length];
33
+        int c = 0;
34
+        for (String anArray : array) {
35
+            if (countDupes(anArray) != exactNumberOfDuplications) {
36
+                stringArr[c] = anArray;
37
+                c++;
38
+            }
39
+        }
40
+        return Arrays.copyOfRange(stringArr,0, c);    }
41
+
42
+    private int countDupes(String str){
43
+        int c = 0;
44
+        for (String index : array){
45
+            if(index.equals(str)){
46
+                c++;
47
+            }
48
+        }
49
+        return c;
50
+    }
8
 }
51
 }

+ 1
- 1
src/test/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleterTest.java 파일 보기

1
 package com.zipcodewilmington.looplabs;
1
 package com.zipcodewilmington.looplabs;
2
 
2
 
3
+import org.junit.Assert;
3
 import org.junit.Before;
4
 import org.junit.Before;
4
 import org.junit.Test;
5
 import org.junit.Test;
5
 
6
 
20
         this.deleter = new IntegerDuplicateDeleter(intArray);
21
         this.deleter = new IntegerDuplicateDeleter(intArray);
21
     }
22
     }
22
 
23
 
23
-
24
     @Test
24
     @Test
25
     public void testRemoveDuplicatesExactlyExactly() {
25
     public void testRemoveDuplicatesExactlyExactly() {
26
         Integer[] expected = new Integer[]{1, 2, 2, 4, 4, 6};
26
         Integer[] expected = new Integer[]{1, 2, 2, 4, 4, 6};