Browse Source

Merge e68abbcd6a9d9088eece7eccdd0a136f925bd895 into 5706be95854cd14023a646b5a08307e1b69df29c

danzcw 6 years ago
parent
commit
6f481b5423
No account linked to committer's email

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

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

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

@@ -1,7 +1,8 @@
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
+import org.junit.Assert;
5 6
 
6 7
 /**
7 8
  * Created by leon on 3/1/18.