vvmk 6 år sedan
förälder
incheckning
974b651315

+ 63
- 2
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Visa fil

@@ -1,7 +1,68 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 /**
4
- * Created by leon on 3/6/18.
6
+ * filename:
7
+ * project: question1
8
+ * author: https://github.com/vvmk
9
+ * date: 3/5/18
5 10
  */
6
-public class ArrayUtility {
11
+public class ArrayUtility<T> {
12
+    private T[] base;
13
+
14
+    public ArrayUtility(T[] base) {
15
+        this.base = base;
16
+    }
17
+
18
+    public int countDuplicatesInMerge(T[] arrayToMerge, T valueToCheck) {
19
+        mergeArrays(arrayToMerge);
20
+        return getNumberOfOccurrences(valueToCheck);
21
+    }
22
+
23
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
24
+        T mostCommon = null;
25
+        int mostOccurrences = 0;
26
+        mergeArrays(arrayToMerge);
27
+        for (T t : base) {
28
+            int count = getNumberOfOccurrences(t);
29
+            if (count > mostOccurrences) {
30
+                mostCommon = t;
31
+                mostOccurrences = count;
32
+            }
33
+        }
34
+        return mostCommon;
35
+    }
36
+
37
+    public int getNumberOfOccurrences(T objectToCheck) {
38
+        int sum = 0;
39
+        for (T t : base) {
40
+            if (objectToCheck.equals(t))
41
+                sum++;
42
+        }
43
+        return sum;
44
+    }
45
+
46
+    public T[] removeValue(T objectToRemove) {
47
+        T[] ret = base;
48
+        for (int i = 0; i < base.length; i++) {
49
+            if (base[i].equals(objectToRemove)) {
50
+                ret = Arrays.copyOf(Arrays.copyOfRange(base, 0, i), base.length - 1);
51
+                System.arraycopy(base, i + 1, ret, i, base.length - i - 1);
52
+                base = ret;
53
+            }
54
+        }
55
+        return ret;
56
+    }
57
+
58
+    protected void mergeArrays(T[] arrayToMerge) {
59
+        int newSize = base.length + arrayToMerge.length;
60
+        base = Arrays.copyOf(base, newSize); // base should be referenceing new array that is identical except length = newSize;
61
+        System.arraycopy(arrayToMerge, 0, base, (newSize - arrayToMerge.length), arrayToMerge.length);
62
+    }
63
+
64
+    public T[] getBase() {
65
+        return base;
66
+    }
7 67
 }
68
+

+ 0
- 2
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java Visa fil

@@ -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
 /**
@@ -58,7 +57,6 @@ public class RemoveValueTest {
58 57
     }
59 58
 
60 59
 
61
-
62 60
     @Test
63 61
     public void objectTest() {
64 62
         // Given