소스 검색

Merge 287d1bc058e4feed86d9f7553c0283a2b79bb9c6 into 5706be95854cd14023a646b5a08307e1b69df29c

Patrick Glavin 6 년 전
부모
커밋
f074377d91
No account linked to committer's email

+ 66
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java 파일 보기

@@ -1,7 +1,72 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.lang.reflect.Array;
4
+import java.util.ArrayList;
5
+import java.util.Arrays;
6
+import java.util.List;
7
+import java.util.TreeMap;
8
+
3 9
 /**
4 10
  * Created by leon on 3/6/18.
5 11
  */
6
-public class ArrayUtility {
12
+public class ArrayUtility <T> {
13
+    private T[] tArray;
14
+
15
+    public ArrayUtility(T[] tArray){
16
+        this.tArray = tArray;
17
+    }
18
+
19
+    public int countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate){
20
+        T[] merged = mergeArrays(arrayToMerge);
21
+        this.tArray = merged;
22
+        return getNumberOfOccurrences(valueToEvaluate);
23
+    }
24
+
25
+    public T getMostCommonFromMerge(T[] arrayToMerge){
26
+        T[] merged = mergeArrays(arrayToMerge);
27
+        int maxDupes = 0;
28
+        T mostCommon = null;
29
+        for (T t:merged) {
30
+            int currentDupes = getNumberOfOccurrencesCustom(t, merged);
31
+            if (currentDupes > maxDupes){
32
+                maxDupes = currentDupes;
33
+                mostCommon = t;
34
+            }
35
+        }
36
+        return mostCommon;
37
+    }
38
+
39
+    public int getNumberOfOccurrences(T duplicatesToCount){
40
+        return getNumberOfOccurrencesCustom(duplicatesToCount, tArray);
41
+    }
42
+
43
+    public int getNumberOfOccurrencesCustom(T duplicatesToCount, T[] testArray){
44
+        int counter = 0;
45
+        for (T t:testArray) {
46
+            if (t.equals(duplicatesToCount)) counter++;
47
+        }
48
+        return counter;
49
+    }
50
+
51
+    public T[] removeValue(T valueToRemove){
52
+        T[] returnArray = Arrays.copyOf(tArray, (tArray.length - this.getNumberOfOccurrences(valueToRemove)));
53
+        int index = 0;
54
+        for (T t:this.tArray) {
55
+            if (!t.equals(valueToRemove)){
56
+                returnArray[index] = t;
57
+                index++;
58
+            }
59
+        }
60
+        return returnArray;
61
+    }
62
+
63
+    public T[] mergeArrays(T[] newArray){
64
+        T[] returnArray = Arrays.copyOf(tArray, (tArray.length + newArray.length));
65
+        int index = tArray.length;
66
+        for (T t:newArray){
67
+            returnArray[index] = t;
68
+            index++;
69
+        }
70
+        return returnArray;
71
+    }
7 72
 }

+ 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.*;
4 4
 import org.junit.Test;
5 5
 
6 6
 /**