Vincent Sima 6 anni fa
parent
commit
8744c5af26

+ 79
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Vedi File

@@ -1,7 +1,85 @@
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
+
3 7
 /**
4 8
  * Created by leon on 3/6/18.
5 9
  */
6
-public class ArrayUtility {
10
+public class ArrayUtility<T> {
11
+    private T[] inputArray;
12
+
13
+
14
+    public ArrayUtility(T[] inputArray) {
15
+        this.inputArray = inputArray;
16
+    }
17
+
18
+    public T[] merge(T[] input) {
19
+        T[] combined = (T[]) new Object[inputArray.length + input.length];
20
+        System.arraycopy(inputArray, 0, combined, 0, inputArray.length);
21
+        System.arraycopy(input, 0, combined, inputArray.length, input.length);
22
+        return combined;
23
+    }
24
+
25
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
26
+
27
+        T[] combined = merge(arrayToMerge);
28
+
29
+
30
+        int count = 0;
31
+        for (T e : combined) {
32
+            if (e == valueToEvaluate) {
33
+                count++;
34
+            }
35
+        }
36
+        return count;
37
+    }
38
+
39
+
40
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
41
+        T[] combined = merge(arrayToMerge);
42
+        int count = 1, tempCount;
43
+        T popular = combined[0];
44
+        T temp;
45
+        for (int i = 0; i < (combined.length - 1); i++)
46
+        {
47
+            temp = combined[i];
48
+            tempCount = 0;
49
+            for (int j = 1; j < combined.length; j++)
50
+            {
51
+                if (temp == combined[j])
52
+                    tempCount++;
53
+            }
54
+            if (tempCount > count)
55
+            {
56
+                popular = temp;
57
+                count = tempCount;
58
+            }
59
+        }
60
+        return popular;
61
+    }
62
+
63
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
64
+        Integer count = 0;
65
+        for(T e : inputArray){
66
+            if(e == valueToEvaluate){
67
+                count++;
68
+            }
69
+        }
70
+        return count;
71
+    }
72
+
73
+    public T[] removeValue(T valueToRemove) {
74
+        int occ = getNumberOfOccurrences(valueToRemove);
75
+        T[] answer = (T[]) Array.newInstance(inputArray.getClass().getComponentType(), inputArray.length-occ);
76
+        int count=0;
77
+        for(int i = 0; i < inputArray.length; i++){
78
+            if(!inputArray[i].equals(valueToRemove)){
79
+                answer[count] = inputArray[i];
80
+                count++;
81
+            }
82
+        }
83
+        return answer;
84
+    }
7 85
 }

+ 0
- 1
src/test/java/com/zipcodewilmington/arrayutility/GetNumberOfOccurrencesTest.java Vedi File

@@ -33,7 +33,6 @@ public class GetNumberOfOccurrencesTest {
33 33
 
34 34
         // When
35 35
         Integer actual = arrayUtility.getNumberOfOccurrences(valueToEvaluate);
36
-
37 36
         // Then
38 37
         Assert.assertEquals(expected, actual);
39 38
     }

+ 1
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java Vedi 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
 /**