Browse Source

Curtis Cook's ArrayUtility Lab

curtiscook 6 years ago
parent
commit
4b1b8b952c

+ 73
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java View File

@@ -1,7 +1,79 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.lang.reflect.Array;
4
+import java.util.ArrayList;
5
+import java.util.Collections;
6
+import java.util.HashMap;
7
+
3 8
 /**
4 9
  * Created by leon on 3/6/18.
5 10
  */
6
-public class ArrayUtility {
11
+public class ArrayUtility<T> {
12
+    private T[] genericList;
13
+
14
+    public ArrayUtility(T[] genericList) {
15
+        this.genericList = genericList;
16
+    }
17
+
18
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
19
+        ArrayList<T> list = new ArrayList<>();
20
+
21
+        Collections.addAll(list, genericList);
22
+        Collections.addAll(list, arrayToMerge);
23
+
24
+        HashMap<T, Integer> map = new HashMap<>();
25
+        for (T type : list) {
26
+            if (map.containsKey(type)) {
27
+                map.put(type, map.get(type) + 1);
28
+            } else {
29
+                map.put(type, 1);
30
+            }
31
+        }
32
+        return map.get(valueToEvaluate);
33
+    }
34
+
35
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
36
+        HashMap<T, Integer> map = new HashMap<>();
37
+        for (T type : arrayToMerge) {
38
+            if (map.containsKey(type)) {
39
+                map.put(type, map.get(type) + 1);
40
+            } else {
41
+                map.put(type, 1);
42
+            }
43
+        }
44
+
45
+        Integer mostCommon = 0;
46
+        T mostCommonT = arrayToMerge[0];
47
+        for (T type : map.keySet()) {
48
+            if (map.get(type) > mostCommon) {
49
+                mostCommon = map.get(type);
50
+                mostCommonT = type;
51
+            }
52
+        }
53
+        return mostCommonT;
54
+    }
55
+
56
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
57
+        Integer count = 0;
58
+        for (T type : genericList) {
59
+            if (type.equals(valueToEvaluate)) {
60
+                count++;
61
+            }
62
+        }
63
+        return count;
64
+    }
65
+
66
+    public T[] removeValue(T valueToRemove) {
67
+        ArrayList<T> arrayList = new ArrayList<>();
68
+        for (T type : genericList) {
69
+            if (!type.equals(valueToRemove)) {
70
+                arrayList.add(type);
71
+            }
72
+        }
73
+        T[] updatedArray = (T[]) Array.newInstance(this.genericList.getClass().getComponentType(), arrayList.size());
74
+        for (int i = 0; i < updatedArray.length; i++) {
75
+            updatedArray[i] = arrayList.get(i);
76
+        }
77
+        return updatedArray;
78
+    }
7 79
 }

+ 1
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java View File

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