Peter McCormick vor 6 Jahren
Ursprung
Commit
b3fdba18d6

+ 76
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Datei anzeigen

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

+ 0
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java Datei anzeigen

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