diff --git a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java index ee550c5..98ae491 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java @@ -1,8 +1,30 @@ package com.zipcodewilmington.looplabs; +import java.util.Arrays; +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); + } + + @Override + public Integer[] removeDuplicates(int maxNumberOfDuplications) { + return Arrays.stream(array).filter(y -> getOccurence(y) < maxNumberOfDuplications).toArray(Integer [] :: new); //last part creates a new array. lambda kind of like for : each + } + + @Override + public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) { + return Arrays.stream(array).filter(t -> getOccurence(t) != exactNumberOfDuplications).toArray((Integer [] :: new)); + } + + //Method we created to find the number of occurences of a particular integer in the stream. + public Long getOccurence (Integer x) { + return Arrays.stream(array).filter(z -> z==x).count(); + } } diff --git a/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java index 4818fe3..c6bdff1 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java @@ -1,8 +1,27 @@ package com.zipcodewilmington.looplabs; +import java.util.Arrays; + /** * 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[] intArray) { + super(intArray); + } + + @Override + public String[] removeDuplicates(int maxNumberOfDuplications) { + return Arrays.stream(array).filter(y -> getOccurence(y) < maxNumberOfDuplications).toArray(String [] :: new); + } + + @Override + public String[] removeDuplicatesExactly(int exactNumberOfDuplications) { + return Arrays.stream(array).filter(t -> getOccurence(t) != exactNumberOfDuplications).toArray((String [] :: new)); + } + + public Long getOccurence (String x) { + return Arrays.stream(array).filter(z -> z.equals(x)).count(); + } } diff --git a/src/test/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleterTest.java b/src/test/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleterTest.java index 3388c27..fd529e6 100644 --- a/src/test/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleterTest.java +++ b/src/test/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleterTest.java @@ -62,20 +62,6 @@ public void testRemoveDuplicatesExactly3() { - - - - - - - - - - - - - -