Eric Foster пре 6 година
родитељ
комит
1eb457edc3

+ 65
- 4
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Прегледај датотеку

@@ -1,7 +1,68 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
-/**
4
- * Created by leon on 3/6/18.
5
- */
6
-public class ArrayUtility {
3
+import java.lang.reflect.Array;
4
+import java.util.Arrays;
5
+
6
+public class ArrayUtility<T> {
7
+
8
+    private T[] inputArray;
9
+
10
+    public ArrayUtility(T[] inputArray) {
11
+        this.inputArray = inputArray;
12
+    }
13
+
14
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
15
+        Integer count = 0;
16
+        count += getNumberOfOccurrences(valueToEvaluate);
17
+        for (T t : arrayToMerge){
18
+            if (t.equals(valueToEvaluate)){
19
+                count++;
20
+            }
21
+        }
22
+        return count;
23
+    }
24
+
25
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
26
+        Integer count = 0;
27
+        for (T t : inputArray){
28
+            if (t.equals(valueToEvaluate)){
29
+                count++;
30
+            }
31
+        }
32
+        return count;
33
+    }
34
+
35
+    public T[] removeValue(T valueToRemove) {
36
+        Integer count = getNumberOfOccurrences(valueToRemove);
37
+        T[] arr = (T[]) Array.newInstance(inputArray.getClass().getComponentType(), inputArray.length-count);
38
+        for (int i=0, j=0; i<inputArray.length-1; i++){
39
+            if (!(inputArray[i].equals(valueToRemove))){
40
+                arr[j] = inputArray[i];
41
+                j++;
42
+            }
43
+        }
44
+        return arr;
45
+    }
46
+
47
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
48
+        T[] mergedArr = mergeArray(arrayToMerge);
49
+        T mostCommonT = mergedArr[0];
50
+        int count = 1;
51
+        for (T t : mergedArr){
52
+            if (getNumberOfOccurrences(t) > count){
53
+                count = getNumberOfOccurrences(t);
54
+                mostCommonT = t;
55
+            }
56
+        }
57
+        return mostCommonT;
58
+    }
59
+
60
+    public T[] mergeArray(T[] arrayToMerge){
61
+        T[] mergedArr = Arrays.copyOf(inputArray, inputArray.length + arrayToMerge.length);
62
+        for (int i=0; i<arrayToMerge.length-1; i++){
63
+            mergedArr[i+inputArray.length] = arrayToMerge[i];
64
+        }
65
+        return mergedArr;
66
+    }
67
+
7 68
 }

+ 1
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java Прегледај датотеку

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