Ben Blinebury il y a 6 ans
Parent
révision
dc16a615b2

+ 80
- 0
IntegerDuplicateDeleter.java Voir le fichier

@@ -2,5 +2,85 @@
2 2
 
3 3
 
4 4
 public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
5
+    
6
+    public IntegerDuplicateDeleter(Integer[] array) {
7
+        super(array);
8
+    }
9
+  
10
+    public Integer[] removeDuplicates(int maxNumberOfDuplications) {
11
+        StringBuilder sb = new StringBuilder();
12
+        
13
+        
14
+        
15
+        for (int i = 0; i < array.length; i++) {
16
+            
17
+            if(getCount(array[i]) < maxNumberOfDuplications){
18
+                
19
+                sb.append((array[i]) + " ");
20
+            }
21
+        }
22
+         String arrString = sb.toString();
23
+         String [] arrPlus = arrString.split(" ");
24
+         Integer[] newArr = new Integer[arrPlus.length];
25
+         
26
+         for (int i = 0; i < newArr.length; i++) {
27
+              try {newArr[i] = Integer.parseInt(arrPlus[i]);}
28
+              catch (NumberFormatException e){}
29
+            }
30
+        
31
+            Integer[] empty = new Integer[]{};
32
+            
33
+         if (newArr.length == 1 && newArr[0] == null) {
34
+             return empty;
35
+            } else { return newArr;}
36
+        
37
+          
38
+    }
39
+    
40
+    public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
41
+        StringBuilder sb = new StringBuilder();
42
+        
43
+        String tester = array.toString();
44
+        
45
+        for (int i = 0; i < array.length; i++) {
46
+            
47
+            if(getCount(array[i]) < exactNumberOfDuplications || getCount(array[i]) 
48
+            > exactNumberOfDuplications) {
49
+                
50
+                sb.append((array[i]) + " ");
51
+            }
52
+        }
53
+         String arrString = sb.toString();
54
+         String [] arrPlus = arrString.split(" ");
55
+         Integer[] newArr = new Integer[arrPlus.length];
56
+         
57
+         for (int i = 0; i < newArr.length; i++) {
58
+              try {newArr[i] = Integer.parseInt(arrPlus[i]);}
59
+              catch (NumberFormatException e){}
60
+            }
61
+        
62
+            Integer[] empty = new Integer[]{};
63
+            
64
+         if (newArr.length == 1 && newArr[0] == null) {
65
+             return empty;
66
+            } else { return newArr;}
67
+        
68
+        
69
+    }
5 70
 
71
+    
72
+    private int getCount (Integer value){
73
+        int count = 0; 
74
+        
75
+        
76
+        for (Integer e : array){
77
+            
78
+            if (e == value) {
79
+                
80
+                count++;
81
+            
82
+            }
83
+        }
84
+        return count;
85
+    }
6 86
 }

+ 1
- 1
IntegerDuplicateDeleterTest.java Voir le fichier

@@ -98,7 +98,7 @@ public class IntegerDuplicateDeleterTest {
98 98
         DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
99 99
 
100 100
         Integer[] expected = new Integer[]{};
101
-        Integer[] actual = deleter.removeDuplicatesExactly(1);
101
+        Integer[] actual = deleter.removeDuplicates(1);
102 102
         Assert.assertArrayEquals(expected, actual);
103 103
     }
104 104
 

+ 67
- 1
StringDuplicateDeleter.java Voir le fichier

@@ -2,5 +2,71 @@
2 2
 
3 3
 public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
4 4
 
5
-
5
+    public StringDuplicateDeleter(String[] array) {
6
+        super(array);
7
+    }
8
+   
9
+    public String[] removeDuplicates(int maxNumberOfDuplications) {
10
+        StringBuilder sb = new StringBuilder();
11
+        
12
+        
13
+        
14
+        for (int i = 0; i < array.length; i++) {
15
+            
16
+            if(getCount(array[i]) < maxNumberOfDuplications){
17
+                
18
+                sb.append((array[i]) + "~");
19
+            }
20
+        }
21
+         String[] arrPlus;
22
+         
23
+         if (sb.length() > 0) {
24
+             arrPlus = sb.toString().split("~");
25
+            } else { arrPlus = new String[] {};
26
+        }
27
+            
28
+         return arrPlus;
29
+    }
30
+    
31
+    public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
32
+        StringBuilder sb = new StringBuilder();
33
+        
34
+        
35
+        
36
+        for (int i = 0; i < array.length; i++) {
37
+            
38
+            if(getCount(array[i]) < exactNumberOfDuplications ||
39
+            getCount(array[i]) > exactNumberOfDuplications){
40
+                
41
+                sb.append((array[i]) + "~");
42
+            }
43
+        }
44
+         
45
+         
46
+         String[] arrPlus;
47
+         
48
+         if (sb.length() > 0) {
49
+             arrPlus = sb.toString().split("~");
50
+            } else { arrPlus = new String[] {};
51
+        }
52
+         
53
+        
54
+        return arrPlus;  
55
+    }
56
+    
57
+    
58
+    private int getCount (String value){
59
+        int count = 0; 
60
+        
61
+        
62
+        for (String e : array){
63
+            
64
+            if (e.equals(value)) {
65
+                
66
+                count++;
67
+            
68
+            }
69
+        }
70
+        return count;
71
+    }
6 72
 }

+ 1
- 1
StringDuplicateDeleterTest.java Voir le fichier

@@ -98,7 +98,7 @@ public class StringDuplicateDeleterTest {
98 98
         DuplicateDeleter<String> deleter = new StringDuplicateDeleter(array);
99 99
 
100 100
         String[] expected = new String[]{};
101
-        String[] actual = deleter.removeDuplicatesExactly(1);
101
+        String[] actual = deleter.removeDuplicates(1);
102 102
         Assert.assertArrayEquals(expected, actual);
103 103
     }
104 104