ソースを参照

most common test

Lauren Green 6 年 前
コミット
bebf1953ae
共有1 個のファイルを変更した25 個の追加1 個の削除を含む
  1. 25
    1
      src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java

+ 25
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java ファイルの表示

@@ -2,6 +2,8 @@ package com.zipcodewilmington.arrayutility;
2 2
 
3 3
 import java.lang.reflect.Array;
4 4
 import java.util.ArrayList;
5
+import java.util.HashMap;
6
+import java.util.Map;
5 7
 
6 8
 /**
7 9
  * Created by leon on 3/6/18.
@@ -16,12 +18,34 @@ public class ArrayUtility<T> {
16 18
 
17 19
     public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
18 20
 
21
+
19 22
         return null;
20 23
     }
21 24
 
22 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 51
     public Integer getNumberOfOccurrences(T valueToEvaluate) {