diff --git a/src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java b/src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java index ca32e28..3ddb20c 100644 --- a/src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java +++ b/src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java @@ -1,7 +1,68 @@ package com.zipcodewilmington.arrayutility; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; + /** * Created by leon on 3/6/18. */ -public class ArrayUtility { +public class ArrayUtility { + Object[] objects; + + public ArrayUtility(Object[] objects){ + this.objects= objects; + + } + + + public int countDuplicatesInMerge(Object[] arrayToMerge, Object valueToEvaluate) { + ArrayList added = new ArrayList<>(Arrays.asList(objects)); + int numOfOccurences = 0; + Collections.addAll(added, arrayToMerge); + + for (Object o : added) { + if (o == valueToEvaluate){ + numOfOccurences++; + } + } + return numOfOccurences; + +// int addLength= objects.length + arrayToMerge.length; +// Object[] merged = Arrays.copyOf(objects, addLength); +// + } + + public Object getMostCommonFromMerge(Object[] arrayToMerge) { + ArrayList added = new ArrayList<>(Arrays.asList(objects)); + Collections.addAll(added, arrayToMerge); + + Object mostPop= null; + int most = Integer.MIN_VALUE; + for (Object object : added) { + if (getNumberOfOccurrences(object) > most){ + mostPop = object; + most = getNumberOfOccurrences(object); + } + } + return mostPop; + } + + public int getNumberOfOccurrences(Object valueToEvaluate) { + int numOfOccurences = 0; + for (Object obj : objects) { + if (obj == valueToEvaluate) { + numOfOccurences++; + } + } + return numOfOccurences; + } + + public Object[] removeValue(Object valueToRemove) { + ArrayList newArray = new ArrayList<>(Arrays.asList(objects)); + newArray.removeAll(Collections.singleton(valueToRemove)); + Object[] edit = (Object[]) newArray.toArray(); + + return edit; + } } diff --git a/src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java b/src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java index d464896..8604bfd 100644 --- a/src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java +++ b/src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java @@ -1,6 +1,6 @@ package com.zipcodewilmington.arrayutility; -import com.zipcodewilmington.UnitTestingUtils; +import com.zipcodewilmington.arrayutility.UnitTestingUtils; import org.junit.Test; /** @@ -18,7 +18,7 @@ public void integerTest() { // When - Integer[] actual = arrayUtility.removeValue(valueToRemove); + Object[] actual = arrayUtility.removeValue(valueToRemove); // Then UnitTestingUtils.assertArrayEquality(expected, actual); @@ -34,7 +34,7 @@ public void longTest() { // When - Long[] actual = arrayUtility.removeValue(valueToRemove); + Object[] actual = arrayUtility.removeValue(valueToRemove); // Then UnitTestingUtils.assertArrayEquality(expected, actual); @@ -51,7 +51,7 @@ public void stringTest() { // When - String[] actual = arrayUtility.removeValue(valueToRemove); + Object[] actual = arrayUtility.removeValue(valueToRemove); // Then UnitTestingUtils.assertArrayEquality(expected, actual);