BlackJack 6 years ago
parent
commit
d6457a249c

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

@@ -1,7 +1,84 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.lang.reflect.Array;
4
+import java.util.*;
5
+
3 6
 /**
4 7
  * Created by leon on 3/6/18.
5 8
  */
6
-public class ArrayUtility {
9
+public class ArrayUtility<I> {
10
+
11
+    private I[] inputArray;
12
+
13
+    public ArrayUtility(I[] inputArray) {
14
+        this.inputArray = inputArray;
15
+    }
16
+
17
+    public I[] removeValue(I valueToRemove) {
18
+        List<I> arr = Arrays.asList(inputArray);
19
+
20
+        ArrayList<I> arr2 = new ArrayList(arr);
21
+        for (Iterator<I> it = arr.iterator(); it.hasNext(); ) {
22
+            I o = it.next();
23
+            if (o.equals(valueToRemove)) {
24
+                arr2.remove(o);
25
+
26
+            }
27
+        }
28
+        I[] arr3 = (I[]) Array.newInstance(inputArray.getClass().getComponentType(), arr2.size());
29
+        return  arr2.toArray(arr3);
30
+
31
+    }
32
+
33
+    public Integer countDuplicatesInMerge(I[] arrayToMerge, I valueToEvaluate) {
34
+        Object[] newArr = new Object[inputArray.length + arrayToMerge.length];
35
+        System.arraycopy(inputArray, 0, newArr, 0, inputArray.length);
36
+        System.arraycopy(arrayToMerge, 0, newArr, inputArray.length, arrayToMerge.length);
37
+
38
+        Integer count = 0;
39
+        for (Object i : newArr) {
40
+            if (i == valueToEvaluate) {
41
+                count++;
42
+            }
43
+        }
44
+        System.out.println(newArr);
45
+        return count;
46
+
47
+    }
48
+
49
+    public I getMostCommonFromMerge(I[] arrayToMerge) {
50
+        Object[] newArr = new Object[inputArray.length + arrayToMerge.length];
51
+        System.arraycopy(inputArray, 0, newArr, 0, inputArray.length);
52
+        System.arraycopy(arrayToMerge, 0, newArr, inputArray.length, arrayToMerge.length);
53
+
54
+        Integer count = 1;
55
+        Integer tempCount;
56
+        Object common = newArr[0];
57
+        Object temp;
58
+
59
+        for (int x = 0; x < newArr.length -1; x++) {
60
+            temp = newArr[x];
61
+            tempCount = 0;
62
+            for (int i = 1; i < newArr.length; i++) {
63
+                if (temp == newArr[i])
64
+                    tempCount++;
65
+                if (count < tempCount) {
66
+                    common = temp;
67
+                    count = tempCount;
68
+                }
69
+            }
70
+        }
71
+        return (I) common;
72
+    }
73
+
74
+    public Integer getNumberOfOccurrences(I valueToEvaluate) {
75
+        Integer count = 0;
76
+        for (Object i : inputArray){
77
+            if ( i == valueToEvaluate){
78
+                count++;
79
+            }
80
+        }
81
+
82
+        return count;
83
+    }
7 84
 }

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

@@ -32,7 +32,7 @@ public class GetMostCommonFromMergeTest {
32 32
         ArrayUtility<Long> arrayUtility = new ArrayUtility<Long>(inputArray);
33 33
 
34 34
         // When
35
-        Long actual = arrayUtility.getMostCommonFromMerge(arrayToMerge);
35
+        Long actual = Long.valueOf(arrayUtility.getMostCommonFromMerge(arrayToMerge));
36 36
 
37 37
         // Then
38 38
         Assert.assertEquals(expected, actual);
@@ -48,7 +48,7 @@ public class GetMostCommonFromMergeTest {
48 48
         ArrayUtility<String> arrayUtility = new ArrayUtility<String>(inputArray);
49 49
 
50 50
         // When
51
-        String actual = arrayUtility.getMostCommonFromMerge(arrayToMerge);
51
+        String actual = String.valueOf(arrayUtility.getMostCommonFromMerge(arrayToMerge));
52 52
 
53 53
         // Then
54 54
         Assert.assertEquals(expected, actual);

+ 1
- 2
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
 /**
@@ -32,7 +32,6 @@ public class RemoveValueTest {
32 32
         Long[] inputArray = {12L, 2L, valueToRemove, 8L, 4L, 5L, valueToRemove, 0L, 9L, 8L, valueToRemove};
33 33
         ArrayUtility<Long> arrayUtility = new ArrayUtility<Long>(inputArray);
34 34
 
35
-
36 35
         // When
37 36
         Long[] actual = arrayUtility.removeValue(valueToRemove);
38 37