瀏覽代碼

All Test pass

Nicholas Maidanos 6 年之前
父節點
當前提交
0c81e12fdd

+ 86
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java 查看文件

@@ -1,7 +1,92 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.lang.reflect.Array;
4
+import java.util.ArrayList;
5
+import java.util.Arrays;
6
+import java.util.HashMap;
7
+import java.util.Map;
8
+
3 9
 /**
4 10
  * Created by leon on 3/6/18.
5 11
  */
6
-public class ArrayUtility {
12
+public class ArrayUtility<E> {
13
+
14
+    private E[] array;
15
+
16
+    public ArrayUtility(E[] array){
17
+        this.array = array;
18
+    }
19
+
20
+    public Integer countDuplicatesInMerge(E[] arrayToMerge, E valueToEvaluate) {
21
+        int counter = 0;
22
+        for(E e: this.array){
23
+            if(valueToEvaluate.equals(e)){
24
+                counter++;
25
+            }
26
+        }
27
+        for(E e: arrayToMerge){
28
+            if(valueToEvaluate.equals(e)){
29
+                counter++;
30
+            }
31
+        }
32
+        return counter;
33
+    }
34
+
35
+    public Integer getNumberOfOccurrences(E valueToEvaluate) {
36
+        Integer counter = 0;
37
+        for (E e: this.array) {
38
+            if(e.equals(valueToEvaluate)){
39
+                counter++;
40
+            }
41
+        }
42
+        return counter;
43
+    }
44
+
45
+    public E getMostCommonFromMerge(E[] arrayToMerge) {
46
+        HashMap<E,Integer> bin = new HashMap<E,Integer>();
47
+
48
+        for (E e: this.array) {
49
+            if(bin.containsKey(e)){
50
+                bin.put(e ,bin.get(e) + 1);
51
+            } else {
52
+                bin.put(e, 1);
53
+            }
54
+        }
55
+
56
+        Map.Entry<E, Integer> i = null;
57
+
58
+        for (Map.Entry<E, Integer> entry: bin.entrySet()) {
59
+            if (i == null || entry.getValue() > i.getValue()){
60
+                i = entry;
61
+            }
62
+        }
63
+
64
+        return i.getKey();
65
+    }
66
+
67
+    public E[] removeValue(E valueToRemove) {
68
+
69
+        int valCounter = 0;
70
+        for(E e: this.array){
71
+            if(e.equals(valueToRemove)){
72
+                valCounter++;
73
+            }
74
+        }
75
+
76
+        E[] withoutValue = Arrays.copyOf(this.array, this.array.length - valCounter);
77
+
78
+        int index = 0;
79
+
80
+        for(int i = 0; i < this.array.length; i++){
81
+            if(!this.array[i].equals(valueToRemove)){
82
+                withoutValue[index] = this.array[i];
83
+                index++;
84
+            }
85
+        }
86
+
87
+        return withoutValue;
88
+
89
+    }
90
+
91
+
7 92
 }

+ 1
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java 查看文件

@@ -1,6 +1,6 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
-import com.zipcodewilmington.UnitTestingUtils;
3
+//import com.zipcodewilmington.UnitTestingUtils;
4 4
 import org.junit.Test;
5 5
 
6 6
 /**