Browse Source

Merge 2ee129b3ca0383dfa68a9e5593aa38f4ba140cf4 into 5706be95854cd14023a646b5a08307e1b69df29c

Lawrence Wu 6 years ago
parent
commit
b240677a9c
No account linked to committer's email

+ 84
- 2
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java View File

@@ -1,7 +1,89 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.util.*;
4
+
3 5
 /**
4 6
  * Created by leon on 3/6/18.
5 7
  */
6
-public class ArrayUtility {
7
-}
8
+public class ArrayUtility<T> {
9
+
10
+    private T[] inputArray;
11
+    private T[] mergedArray;
12
+
13
+    public ArrayUtility(T[] inputArray) {
14
+        this.inputArray = inputArray;
15
+    }
16
+
17
+    public T[] mergeArrays(T[] inputArray, T[] arrayToMerge) {
18
+        int inputArrayLength = inputArray.length;
19
+        int arrayToMergeLength = arrayToMerge.length;
20
+
21
+        mergedArray = (T[]) new Object[inputArrayLength + arrayToMergeLength];
22
+
23
+        System.arraycopy(inputArray, 0, mergedArray, 0, inputArrayLength);
24
+        System.arraycopy(arrayToMerge, 0, mergedArray, inputArrayLength, arrayToMergeLength);
25
+
26
+        return mergedArray;
27
+    }
28
+
29
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valuetoEvaluate) {
30
+        mergeArrays(inputArray, arrayToMerge);
31
+        int count = 0;
32
+        for (T value : mergedArray) {
33
+            if (value == valuetoEvaluate) {
34
+                count++;
35
+            }
36
+        }
37
+        return count;
38
+    }
39
+
40
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
41
+        mergeArrays(inputArray, arrayToMerge);
42
+        //dont need a map for this but wanted to do some map practice
43
+        HashMap<T, Integer> countingMap = new HashMap<>();
44
+
45
+        for (T occurrence : mergedArray) {
46
+            if (countingMap.containsKey(occurrence)) {
47
+                countingMap.put(occurrence, countingMap.get(occurrence) + 1);
48
+            } else {
49
+                countingMap.put(occurrence, 1);
50
+            }
51
+        }
52
+        T element = null;
53
+        int frequency = 1;
54
+        Set<Map.Entry<T, Integer>> entrySet = countingMap.entrySet();
55
+        for (Map.Entry<T, Integer> entry : entrySet) {
56
+            if (entry.getValue() > frequency) {
57
+                element = entry.getKey();
58
+                frequency = entry.getValue();
59
+            }
60
+        }
61
+        return element;
62
+    }
63
+
64
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
65
+        int count = 0;
66
+        for (T value : inputArray) {
67
+            if (value == valueToEvaluate) {
68
+                count++;
69
+            }
70
+        }
71
+        return count;
72
+    }
73
+
74
+    public T[] removeValue(T valueToRemove) {
75
+        T[] tempArray = inputArray;
76
+
77
+        for (int i = 0; i < inputArray.length; i++) {
78
+            if (inputArray[i].equals(valueToRemove)) {
79
+                //copy up to the found dup
80
+                tempArray = Arrays.copyOf(Arrays.copyOfRange(inputArray, 0, i), inputArray.length - 1);
81
+                //take original, place in temp up to dup
82
+                System.arraycopy(inputArray, i + 1, tempArray, i, inputArray.length - i - 1);
83
+                //go for more dupes
84
+                inputArray = tempArray;
85
+            }
86
+        }
87
+        return tempArray;
88
+    }
89
+}

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

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