Przeglądaj źródła

Passed all tests

Akeem Cherry 6 lat temu
rodzic
commit
77db25c57c

+ 61
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Wyświetl plik

@@ -1,7 +1,67 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.util.*;
4
+import java.util.stream.Stream;
5
+
3 6
 /**
4 7
  * Created by leon on 3/6/18.
5 8
  */
6
-public class ArrayUtility {
9
+public class ArrayUtility<T> {
10
+
11
+    private final T[] array;
12
+
13
+
14
+    public ArrayUtility(T[] inputArray) {
15
+        this.array = inputArray;
16
+    }
17
+
18
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
19
+        Integer count = 0;
20
+        List<T> list1 = new ArrayList<>(Arrays.asList(array));
21
+        List<T> list2 = new ArrayList<>(Arrays.asList(arrayToMerge));
22
+        Stream<T> mergedList = Stream.concat(list1.stream(), list2.stream());
23
+        T[] mergedArr = (T[]) mergedList.toArray();
24
+        for (T value : mergedArr){
25
+            if (value.equals(valueToEvaluate)){
26
+                mergedArr[count] = value;
27
+                count++;
28
+            }
29
+        }
30
+        return count;
31
+    }
32
+
33
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
34
+        T mostCommon = null;
35
+        Integer count = Integer.MIN_VALUE;
36
+        List<T> list1 = new ArrayList<>(Arrays.asList(array));
37
+        List<T> list2 = new ArrayList<>((Arrays.asList(arrayToMerge)));
38
+        Stream<T> mergedList = Stream.concat(list1.stream(), list2.stream());
39
+        T[] mergedArr = (T[]) mergedList.toArray();
40
+        for (T value : mergedArr){
41
+            if (getNumberOfOccurrences(value) > count){
42
+                count = getNumberOfOccurrences(value);
43
+                mostCommon = value;
44
+            }
45
+        }
46
+        return mostCommon;
47
+    }
48
+
49
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
50
+        List<T> a2List = new ArrayList(Arrays.asList(array));
51
+        Integer occurences = Collections.frequency(a2List, valueToEvaluate);
52
+
53
+        return occurences;
54
+    }
55
+
56
+    public T[] removeValue(T valueToRemove) {
57
+        int i=0;
58
+        for (T arr : array){
59
+            if (!arr.equals(valueToRemove)){
60
+                array[i] = arr;
61
+                i++;
62
+            }
63
+        }
64
+        T[] array2 = Arrays.copyOf(array, i);
65
+        return array2;
66
+    }
7 67
 }

+ 0
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java Wyświetl plik

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