Raymond Farria 6 лет назад
Родитель
Сommit
349d92bb72

+ 76
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Просмотреть файл

@@ -1,7 +1,82 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.util.*;
4
+import java.util.stream.Collector;
5
+
3 6
 /**
4 7
  * Created by leon on 3/6/18.
5 8
  */
6
-public class ArrayUtility {
9
+public class ArrayUtility<T> {
10
+private T[] inputArray;
11
+private List<T> arrays = new ArrayList<>();
12
+    private List<T> anotherArray = new ArrayList<>();
13
+
14
+
15
+    public ArrayUtility(T[] inputArray) {
16
+        this.inputArray = inputArray;
17
+    }
18
+
19
+    public T[] removeValue(T valueToRemove) {
20
+        arrays.addAll(Arrays.asList(inputArray));
21
+        arrays.removeIf(t -> t.equals(valueToRemove));
22
+        return arrays.toArray(Arrays.copyOf(inputArray,arrays.size()));
23
+    }
24
+
25
+
26
+
27
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
28
+        arrays.addAll(Arrays.asList(inputArray));
29
+        arrays.addAll(Arrays.asList(arrayToMerge));
30
+
31
+        Object[] temp = arrays.toArray();
32
+
33
+        Object element = null;
34
+        int count = 0;
35
+        for (int i = 0; i < temp.length; i++) {
36
+            Object tempElement = temp[i];
37
+            int tempCount = 0;
38
+            for(int p = 0; p < temp.length; p++){
39
+                if(temp[p].equals(tempElement)){
40
+                    tempCount++;
41
+                }
42
+            }
43
+            if(tempCount>count){
44
+                element = tempElement;
45
+                count = tempCount;
46
+            }
47
+
48
+        }
49
+        return (T)element;
50
+    }
51
+
52
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
53
+        arrays.addAll(Arrays.asList(inputArray));
54
+        int count = 0;
55
+
56
+        for(T t : arrays){
57
+            if(t.equals(valueToEvaluate)){
58
+                count++;
59
+            }
60
+        }
61
+        return count;
62
+    }
63
+
64
+
65
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
66
+        arrays.addAll(Arrays.asList(inputArray));
67
+        arrays.addAll(Arrays.asList(arrayToMerge));
68
+
69
+        int count = 0;
70
+        for(T t : arrays){
71
+            if (t.equals(valueToEvaluate)){
72
+                count++;
73
+            }
74
+        }
75
+        return count;
76
+    }
77
+
78
+
79
+    public T[] getInputArray() {
80
+        return inputArray;
81
+    }
7 82
 }

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