Browse Source

Merge 110feb74ba437668e82dc69a1690fb4a60d50f22 into 5706be95854cd14023a646b5a08307e1b69df29c

Katrice 6 years ago
parent
commit
10b1399597
No account linked to committer's email

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

@@ -1,7 +1,73 @@
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 {
8
+public class ArrayUtility<T> {
9
+    T[] inputArray;
10
+
11
+    public ArrayUtility(T[] inputArray) {
12
+        this.inputArray = inputArray;
13
+    }
14
+
15
+    public T[] mergeArray(T[] inputArray, T[] arrayToMerge){
16
+        T[] mergedArray = Arrays.copyOf(inputArray, inputArray.length + arrayToMerge.length);
17
+
18
+        System.arraycopy(arrayToMerge, 0, mergedArray, inputArray.length, arrayToMerge.length);
19
+
20
+        return mergedArray;
21
+    }
22
+
23
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
24
+        Integer count = 0;
25
+        T[] finalArray = mergeArray(arrayToMerge, inputArray);
26
+        for(int i = 0; i< finalArray.length; i++){
27
+            if(finalArray[i].equals(valueToEvaluate)){
28
+                count ++;
29
+            }
30
+
31
+        }
32
+        return count;
33
+    }
34
+
35
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
36
+        Integer count = 0;
37
+        T obj = null;
38
+        T[] newArray = mergeArray(inputArray, arrayToMerge);
39
+        for(int i = 0; i < newArray.length; i++){
40
+            Integer a = getNumberOfOccurrences(newArray[i]);
41
+            if(a > count){
42
+                count = a;
43
+                obj = newArray[i];
44
+            }
45
+        }
46
+
47
+        return obj;
48
+    }
49
+
50
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
51
+        Integer count = 0;
52
+        for(int i = 0; i<inputArray.length; i++){
53
+            if(inputArray[i].equals(valueToEvaluate)){
54
+                count++;
55
+            }
56
+        }
57
+        return count;
58
+    }
59
+
60
+    public T[] removeValue(T valueToRemove) {
61
+        Integer notDuplicates = 0;
62
+        int newSize = inputArray.length - getNumberOfOccurrences(valueToRemove);
63
+        T[] outputArray;
64
+        outputArray = Arrays.copyOf(inputArray, newSize);
65
+        for(int i = 0; i<inputArray.length; i++){
66
+            if(!inputArray[i].equals(valueToRemove)){
67
+                outputArray[notDuplicates]=inputArray[i];
68
+                notDuplicates++;
69
+            }
70
+        }
71
+        return outputArray;
72
+    }
7 73
 }

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