CWinarski 6 лет назад
Родитель
Сommit
5105f37a68
1 измененных файлов: 32 добавлений и 14 удалений
  1. 32
    14
      src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java

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

@@ -10,34 +10,52 @@ public class ArrayUtility<T> {
10 10
     public T[] inputArray;
11 11
     public T[] mergedArray;
12 12
 
13
-    public ArrayUtility(T[] inputArray){
13
+    public ArrayUtility(T[] inputArray) {
14 14
         this.inputArray = inputArray;
15 15
     }
16 16
 
17 17
     public T getMostCommonFromMerge(T[] arrayToMerge) {
18
-     this.mergedArray = Arrays.copyOf(this.inputArray, this.inputArray.length + arrayToMerge.length);
19
-     System.arraycopy(arrayToMerge, 0, mergedArray, mergedArray.length - arrayToMerge.length, arrayToMerge.length);
20
-     return null;
18
+        Arrays.sort(mergeArrays(arrayToMerge));
19
+        for( )
20
+        return null;
21 21
     }
22 22
 
23 23
     public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
24
-     this.mergedArray = Arrays.copyOf(this.inputArray, this.inputArray.length + arrayToMerge.length);
25
-      System.arraycopy(arrayToMerge, 0, mergedArray, mergedArray.length - arrayToMerge.length, arrayToMerge.length);
26
-       Integer count = 0;
27
-       for (T t : this.mergedArray){
28
-           if (t == valueToEvaluate){
29
-               count++;
30
-           }
31
-       }
24
+        mergeArrays(arrayToMerge);
25
+        Integer count = 0;
26
+        for (T elementInArray : this.mergedArray) {
27
+            if (elementInArray == valueToEvaluate) {
28
+                count++;
29
+            }
30
+        }
32 31
         return count;
33 32
     }
34 33
 
35 34
     public Integer getNumberOfOccurrences(T valueToEvaluate) {
36
-        return null;
35
+        Integer count = 0;
36
+        for (T elementInArray : this.inputArray) {
37
+            if (elementInArray == valueToEvaluate) {
38
+                count++;
39
+            }
40
+        }
41
+        return count;
37 42
     }
38 43
 
39 44
     public T[] removeValue(T valueToRemove) {
40
-           return null;
45
+        T[] arrayWitValueRemoved = Arrays.copyOf(this.inputArray, this.inputArray.length - getNumberOfOccurrences(valueToRemove));
46
+        Integer indexCount = 0;
47
+        for (T elementInArray : this.inputArray) {
48
+            if (valueToRemove != elementInArray) {
49
+                arrayWitValueRemoved[indexCount] = elementInArray;
50
+                indexCount++;
51
+            }
52
+        }
53
+        return arrayWitValueRemoved;
41 54
     }
42 55
 
56
+    public T[] mergeArrays(T[] arrayToMerge) {
57
+        this.mergedArray = Arrays.copyOf(this.inputArray, this.inputArray.length + arrayToMerge.length);
58
+        System.arraycopy(arrayToMerge, 0, mergedArray, mergedArray.length - arrayToMerge.length, arrayToMerge.length);
59
+        return this.mergedArray;
60
+    }
43 61
 }