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
51 changes: 49 additions & 2 deletions src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
package com.zipcodewilmington.arrayutility;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

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

public T countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
ArrayList<T> jawner = new ArrayList<>(Arrays.asList(inputArray));
ArrayList<T> merge = new ArrayList<>(Arrays.asList(arrayToMerge));
jawner.addAll(merge);
Integer result = Collections.frequency(jawner, valueToEvaluate);
return (T) result;
}

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

public int getNumberOfOccurrences(T valueToEvaluate) {
ArrayList<T> jawner = new ArrayList<>(Arrays.asList(inputArray));
Integer result = Collections.frequency(jawner, valueToEvaluate);
return result;
}

public T getMostCommonFromMerge(T[] arrayToMerge) {
ArrayList<T> jawner = new ArrayList<>(Arrays.asList(inputArray));
ArrayList<T> merge = new ArrayList<>(Arrays.asList(arrayToMerge));
jawner.addAll(merge);

T mostJawn = null;
int most = Integer.MIN_VALUE;
for (T object : jawner) {
if (getNumberOfOccurrences(object) > most) {
mostJawn = object;
most = getNumberOfOccurrences(object);
}
}
return mostJawn;
} // Thought process here - sorted the array, use Lamda? maybe? to
} // to check for most commong object
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