Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
package com.zipcodewilmington.arrayutility;

import java.util.*;

/**
* Created by leon on 3/6/18.
*/
public class ArrayUtility {
public class ArrayUtility<T> {
T [] currentArray;

public ArrayUtility(T[] inputArr){
this.currentArray = inputArr;
}

public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
ArrayList<T> mergedList = combine(arrayToMerge);
this.currentArray = (T[]) mergedList.toArray();
Integer count = getNumberOfOccurrences(valueToEvaluate);
return count;
}

public T getMostCommonFromMerge(T[] arrayToMerge) {
ArrayList<T> mergedList = combine(arrayToMerge);
T mostCommon = null;
int mostCount = 0;
for(T element : mergedList) {
Integer currentCount = getNumberOfOccurrences(element);
if(currentCount > mostCount){
mostCount = currentCount;
mostCommon = element;
}
}
return mostCommon;
}

public Integer getNumberOfOccurrences(T valueToEvaluate) {
ArrayList<T> currentList = new ArrayList<>(Arrays.asList(currentArray));
Integer count = Collections.frequency(currentList,valueToEvaluate);
return count;
}

public T[] removeValue(T valueToRemove) {
ArrayList<T> currentList = new ArrayList<>(Arrays.asList(currentArray));
currentList.removeIf(e -> e.equals(valueToRemove));
return (T[]) currentList.toArray();
}

private ArrayList<T> combine(T[] arrayToCombine) {
ArrayList<T> arrToMergeAsList = new ArrayList<>(Arrays.asList(arrayToCombine));
ArrayList<T> currentList = new ArrayList<>(Arrays.asList(currentArray));
currentList.addAll(arrToMergeAsList);
return currentList;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void longTest() {
ArrayUtility<Long> arrayUtility = new ArrayUtility<Long>(inputArray);

// When
Integer actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);
Object actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);

// Then
Assert.assertEquals(expected, actual);
Expand All @@ -51,7 +51,7 @@ public void stringTest() {
ArrayUtility<String> arrayUtility = new ArrayUtility<String>(inputArray);

// When
Integer actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);
Object actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);

// Then
Assert.assertEquals(expected, actual);
Expand All @@ -68,7 +68,7 @@ public void objectTest() {
ArrayUtility<Object> arrayUtility = new ArrayUtility<Object>(inputArray);

// When
Integer actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);
Object actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);

// Then
Assert.assertEquals(expected, actual);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void longTest() {
ArrayUtility<Long> arrayUtility = new ArrayUtility<Long>(inputArray);

// When
Integer actual = arrayUtility.getNumberOfOccurrences(valueToEvaluate);
Object actual = arrayUtility.getNumberOfOccurrences(valueToEvaluate);

// Then
Assert.assertEquals(expected, actual);
Expand All @@ -47,7 +47,7 @@ public void stringTest() {
ArrayUtility<String> arrayUtility = new ArrayUtility<String>(inputArray);

// When
Integer actual = arrayUtility.getNumberOfOccurrences(valueToEvaluate);
Object actual = arrayUtility.getNumberOfOccurrences(valueToEvaluate);

// Then
Assert.assertEquals(expected, actual);
Expand All @@ -62,7 +62,7 @@ public void objectTest() {
ArrayUtility<Object> arrayUtility = new ArrayUtility<Object>(inputArray);

// When
Integer actual = arrayUtility.getNumberOfOccurrences(valueToEvaluate);
Object actual = arrayUtility.getNumberOfOccurrences(valueToEvaluate);

// Then
Assert.assertEquals(expected, actual);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.zipcodewilmington.arrayutility;

import com.zipcodewilmington.UnitTestingUtils;
import com.zipcodewilmington.arrayutility.UnitTestingUtils;
import org.junit.Test;

/**
Expand All @@ -18,7 +18,7 @@ public void integerTest() {


// When
Integer[] actual = arrayUtility.removeValue(valueToRemove);
Object[] actual = arrayUtility.removeValue(valueToRemove);

// Then
UnitTestingUtils.assertArrayEquality(expected, actual);
Expand All @@ -34,7 +34,7 @@ public void longTest() {


// When
Long[] actual = arrayUtility.removeValue(valueToRemove);
Object[] actual = arrayUtility.removeValue(valueToRemove);

// Then
UnitTestingUtils.assertArrayEquality(expected, actual);
Expand All @@ -51,7 +51,7 @@ public void stringTest() {


// When
String[] actual = arrayUtility.removeValue(valueToRemove);
Object[] actual = arrayUtility.removeValue(valueToRemove);

// Then
UnitTestingUtils.assertArrayEquality(expected, actual);
Expand Down