瀏覽代碼

Merge 7859083da9df7f10ec18dfddaeeade4f449d2fcd into 5706be95854cd14023a646b5a08307e1b69df29c

Brian 6 年之前
父節點
當前提交
fbc9989fd9
沒有帳戶連結到提交者的電子郵件

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

@@ -1,7 +1,95 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+
4
+import java.util.Arrays;
5
+import java.util.Collections;
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 <T> {
13
+
14
+    private T[] array;
15
+
16
+    public ArrayUtility (T[] array) {
17
+        this.array = array;
18
+    }
19
+
20
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
21
+        Integer counter = 0;
22
+
23
+        for(int i = 0; i < array.length; i++ ) {
24
+            if(array[i] == valueToEvaluate) {
25
+                counter++;
26
+            }
27
+        }
28
+        return counter;
29
+    }
30
+
31
+    public Integer getNumberOfOccurrencesMerged(T[] arrayToMerge, T valueToEvaluate) {
32
+        T[] mergedArray = mergeArrays(arrayToMerge);
33
+        Integer counter = 0;
34
+        for(int i = 0; i < mergedArray.length; i++) {
35
+            if(mergedArray[i] == valueToEvaluate) {
36
+                counter++;
37
+            }
38
+        }
39
+        return counter;
40
+    }
41
+
42
+    public T[] mergeArrays(T[] arrayToMerge) {
43
+        T[] mergedArray = Arrays.copyOf(this.array, this.array.length + arrayToMerge.length);
44
+        System.arraycopy(arrayToMerge, 0, mergedArray, this.array.length, arrayToMerge.length);
45
+        return mergedArray;
46
+    }
47
+
48
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
49
+        T[] mergedArray = mergeArrays(arrayToMerge);
50
+        Integer counter = 0;
51
+
52
+        for(int i = 0; i < mergedArray.length; i++) {
53
+            if(mergedArray[i] == valueToEvaluate) {
54
+                counter++;
55
+            }
56
+        }
57
+        return counter;
58
+    }
59
+
60
+    public T getMostCommon(T[] arrayToMerge) {
61
+        T[] mergedArray = mergeArrays(arrayToMerge);
62
+        Map<T, Integer> countMap = new HashMap<>();
63
+        for(T key : mergedArray) {
64
+            if(countMap.get(key) == null) {
65
+                countMap.put(key, getNumberOfOccurrencesMerged(arrayToMerge, key));
66
+            }
67
+        }
68
+
69
+        int max = Collections.max(countMap.values());
70
+        T mostCommon = null;
71
+
72
+        for(Map.Entry<T, Integer> entry : countMap.entrySet()) {
73
+            if(entry.getValue() == max) {
74
+                mostCommon = entry.getKey();
75
+            }
76
+        }
77
+        return mostCommon;
78
+    }
79
+
80
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
81
+        return getMostCommon(arrayToMerge);
82
+    }
83
+
84
+    public T[] removeValue(T valueToRemove) {
85
+        T[] newArray = Arrays.copyOf(this.array, array.length - getNumberOfOccurrences(valueToRemove));
86
+        for(int i = 0, j = 0; i < this.array.length; i++) {
87
+            if(array[i] != valueToRemove) {
88
+                newArray[j] = array[i];
89
+                j++;
90
+            }
91
+        }
92
+        return newArray;
93
+    }
94
+
7 95
 }

+ 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
 /**