Aleena Rose-Mathew 6 years ago
parent
commit
43044005a2
1 changed files with 16 additions and 9 deletions
  1. 16
    9
      src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java

+ 16
- 9
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java View File

@@ -8,11 +8,13 @@ import java.util.Arrays;
8 8
  * Created by leon on 3/6/18.
9 9
  */
10 10
 public class ArrayUtility<S> {
11
-    ArrayList<S> newlist=new ArrayList<S>();
11
+
12 12
     S[] inputArray;
13
+    S[] mergerArray;
14
+
13 15
     public ArrayUtility(S[] inputArray) {
14 16
         this.inputArray=inputArray;
15
-        this.newlist.addAll(Arrays.asList(inputArray));
17
+
16 18
         //S[] temp = (S[]) new Object[length];(creating a new generic array)
17 19
     }
18 20
     public int getNumberOfOccurrences(S valueToEvaluate)
@@ -45,10 +47,13 @@ public class ArrayUtility<S> {
45 47
 
46 48
     }
47 49
 
48
-    public Integer countDuplicatesInMerge(S[] inputArray, S valueToEvaluate ) {
50
+    public Integer countDuplicatesInMerge(S[] newArray, S valueToEvaluate ) {
51
+
49 52
         Integer count=0;
50
-        newlist.addAll(Arrays.asList(inputArray));
51
-        for (S element : newlist) {
53
+        mergerArray = (S[]) new Object[inputArray.length+newArray.length];
54
+        System.arraycopy(inputArray, 0,mergerArray, 0,inputArray.length);
55
+        System.arraycopy(newArray,0,mergerArray, inputArray.length,newArray.length);
56
+        for (S element : mergerArray) {
52 57
             if(element.equals(valueToEvaluate))
53 58
             {
54 59
                 count++;
@@ -57,11 +62,14 @@ public class ArrayUtility<S> {
57 62
         return count;
58 63
     }
59 64
 
60
-    public S getMostCommonFromMerge(S[] inputArray)
65
+    public S getMostCommonFromMerge(S[] newArray)
61 66
     {
62
-        S mostcommonElement=inputArray[0];
67
+        mergerArray = (S[]) new Object[inputArray.length+newArray.length];
68
+        System.arraycopy(inputArray, 0,mergerArray, 0,inputArray.length);
69
+        System.arraycopy(newArray,0,mergerArray, inputArray.length,newArray.length);
70
+        S mostcommonElement=mergerArray[0];
63 71
         int mostcommonElementOccurences=0;
64
-        for(S element :newlist)
72
+        for(S element :mergerArray)
65 73
         {
66 74
             if(getNumberOfOccurrences(element)>mostcommonElementOccurences)
67 75
             {
@@ -71,5 +79,4 @@ public class ArrayUtility<S> {
71 79
         }
72 80
         return mostcommonElement;
73 81
     }
74
-
75 82
 }