shakila 6 vuotta sitten
vanhempi
commit
1548f449ec
1 muutettua tiedostoa jossa 53 lisäystä ja 31 poistoa
  1. 53
    31
      src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java

+ 53
- 31
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Näytä tiedosto

@@ -1,53 +1,75 @@
1 1
 package com.zipcodewilmington.arrayutility;
2
-import java.util.Arrays;
3
-import java.util.HashMap;
4
-import java.util.stream.Stream;
2
+import java.util.*;
5 3
 
6 4
 public class ArrayUtility <T> {
7 5
     private T[] inputArray;
6
+    private T[] mergeArray;
7
+    private Object valueToRemove;
8 8
 
9 9
     public ArrayUtility(){}
10 10
     public ArrayUtility(T[] inputArray) {
11
-        this.inputArray = (T[]) inputArray;
11
+        this.inputArray =  inputArray;
12 12
     }
13 13
 
14 14
     public T getMostCommonFromMerge(T[] arrayToMerge) {
15
-        T[] mergeArray = Arrays.copyOf(inputArray, inputArray.length + arrayToMerge.length);
16
-        for(int i = 0; i<mergeArray.length; i++){
17
-            mergeArray[i+ inputArray.length] = arrayToMerge[i];
15
+        mergeArray = (T[]) new Object[inputArray.length + arrayToMerge.length];
16
+       System.arraycopy(inputArray, 0,mergeArray, 0, inputArray.length);
17
+       System.arraycopy(arrayToMerge, 0, mergeArray, inputArray.length, arrayToMerge.length);
18
+       // System.out.println(mergeArray);
19
+
20
+        int numberOfOccurences = 0;
21
+        T mostCommon = mergeArray[0];
22
+        for (T element : mergeArray)
23
+        {
24
+            if (getNumberOfOccurrences(element) > numberOfOccurences)
25
+            {
26
+                mostCommon = element;
27
+                numberOfOccurences = getNumberOfOccurrences(element);
28
+            }
29
+
18 30
         }
31
+        return mostCommon;
32
+    }
19 33
 
20
-        HashMap<T, Integer> repeats = new HashMap<>();
21
-        Integer mostCommon = 0;
22
-        for (int i = 0; i < mergeArray.length; i++) {
23
-            int count = 0;
24
-            for (int j = 0; j < mergeArray.length; j++) {
25
-                if (mergeArray[i] == mergeArray[j]) {
26
-                    count++;
34
+
35
+        public T[] removeValue (T valueToRemove){
36
+        Integer appearances = getNumberOfOccurrences(valueToRemove);
37
+            int i = 0;
38
+            //mergeArray = (T[]) new Object[inputArray.length-appearances];
39
+             mergeArray = Arrays.copyOf(inputArray, inputArray.length-appearances);
40
+            for (T element : inputArray) {
41
+                if (!element.equals(valueToRemove)) {
42
+                    mergeArray[i] = element;
43
+                    i++;
44
+                }
27 45
                 }
46
+                return mergeArray;
28 47
             }
29 48
 
30
-            if (count > 1) {
31
-                if (!repeats.containsKey(mergeArray[i])) {
32
-                    repeats.put(mergeArray[i], count);
33
-                    mostCommon += count;
49
+
50
+        public Integer countDuplicatesInMerge (T[]arrayToMerge, T valueToEvaluate){
51
+            mergeArray = (T[]) new Object[inputArray.length + arrayToMerge.length];
52
+            System.arraycopy(inputArray, 0,mergeArray, 0, inputArray.length);
53
+            System.arraycopy(arrayToMerge, 0, mergeArray, inputArray.length, arrayToMerge.length);
54
+            Integer appearances = 0;
55
+            for (T element : mergeArray) {
56
+                if (element == valueToEvaluate) {
57
+                    appearances++;
34 58
                 }
35 59
             }
60
+            return appearances;
36 61
         }
37
-        T dupe = (T) repeats.get(0);
38
-        return dupe;
39
-    }
40 62
 
41 63
 
42
-    public T[] removeValue(T valueToRemove) {
43
-        return (T[]) inputArray;
44
-    }
45
-
46
-    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
47
-        return null;
48
-    }
49
-
50 64
     public Integer getNumberOfOccurrences(T valueToEvaluate) {
51
-        return null;
65
+       // mergeArray = Arrays.copyOf(inputArray, inputArray.length);
66
+        Integer appearances = 0;
67
+        for (T element : inputArray) {
68
+            if (element == valueToEvaluate) {
69
+                appearances++;
70
+            }
71
+        }
72
+        return appearances;
52 73
     }
53
-}
74
+
75
+    }//end of Class