Explorar el Código

most common test

Lauren Green hace 6 años
padre
commit
bebf1953ae

+ 25
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Ver fichero

2
 
2
 
3
 import java.lang.reflect.Array;
3
 import java.lang.reflect.Array;
4
 import java.util.ArrayList;
4
 import java.util.ArrayList;
5
+import java.util.HashMap;
6
+import java.util.Map;
5
 
7
 
6
 /**
8
 /**
7
  * Created by leon on 3/6/18.
9
  * Created by leon on 3/6/18.
16
 
18
 
17
     public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
19
     public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
18
 
20
 
21
+
19
         return null;
22
         return null;
20
     }
23
     }
21
 
24
 
22
     public T getMostCommonFromMerge(T[] arrayToMerge) {
25
     public T getMostCommonFromMerge(T[] arrayToMerge) {
23
 
26
 
24
-        return null;
27
+        T mostCommon = null;
28
+        Integer highValue = 0;
29
+        Map<T, Integer> counts = new HashMap<>();
30
+
31
+        for (T data: array) {
32
+            if(counts.containsKey(data)) {
33
+                int currentAmount = counts.get(data);
34
+                currentAmount++;
35
+                counts.put(data, currentAmount);
36
+            } else {
37
+                counts.put(data, 1);
38
+            }
39
+        }
40
+
41
+        for (T key: counts.keySet()) {
42
+            if(counts.get(key) > highValue) {
43
+                highValue = counts.get(key);
44
+                mostCommon = key;
45
+            }
46
+        }
47
+
48
+        return mostCommon;
25
     }
49
     }
26
 
50
 
27
     public Integer getNumberOfOccurrences(T valueToEvaluate) {
51
     public Integer getNumberOfOccurrences(T valueToEvaluate) {