diff --git a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java index ee550c5..2cad9e0 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java @@ -1,8 +1,38 @@ package com.zipcodewilmington.looplabs; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + /** * Created by leon on 1/29/18. * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class. */ public final class IntegerDuplicateDeleter extends DuplicateDeleter { + public IntegerDuplicateDeleter(Integer[] intArray) { + super(intArray); + } + + public Integer[] removeDuplicates(int maxNumberOfDuplications) { + return Arrays.stream(array) + .filter(element -> getNumberOfOccurrances(element) < maxNumberOfDuplications) + .collect(Collectors.toList()) + .toArray(new Integer[0]); + } + + public long getNumberOfOccurrances(Integer element) { + return (Arrays.stream(array) + .filter(num -> num == element) + .count()); + } + + public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) { + return Arrays.stream(array) + .filter(element -> getNumberOfOccurrances(element) != exactNumberOfDuplications) + .collect(Collectors.toList()) + .toArray(new Integer[0]); + } } diff --git a/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java index 4818fe3..9a16e4e 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java @@ -1,8 +1,35 @@ package com.zipcodewilmington.looplabs; +import java.util.Arrays; +import java.util.stream.Collectors; + /** * Created by leon on 1/28/18. * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class. */ public final class StringDuplicateDeleter extends DuplicateDeleter { + public StringDuplicateDeleter(String[] stringArray) { + super(stringArray); + } + + public long getNumberOfOccurrances(String element) { + return (Arrays.stream(array) + .filter(word -> word.equals(element)) + .count()); + } + + public String[] removeDuplicates(int maxNumberOfDuplications) { + + return Arrays.stream(array) + .filter(element -> getNumberOfOccurrances(element) < maxNumberOfDuplications) + .collect(Collectors.toList()) + .toArray(new String[0]); + } + + public String[] removeDuplicatesExactly(int exactNumberOfDuplications) { + return Arrays.stream(array) + .filter(element -> getNumberOfOccurrances(element) != exactNumberOfDuplications) + .collect(Collectors.toList()) + .toArray(new String[0]); + } }