From a837233ca9f4a3e645569753a89344f9bd90dea6 Mon Sep 17 00:00:00 2001 From: wes Date: Thu, 14 Nov 2019 19:10:21 -0500 Subject: [PATCH 1/4] project now compiles --- .../looplabs/IntegerDuplicateDeleter.java | 11 +++++++++++ .../looplabs/StringDuplicateDeleter.java | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java index ee550c5..0c22a16 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java @@ -5,4 +5,15 @@ * @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 null; + } + + public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) { + return null; + } } diff --git a/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java index 4818fe3..90ccd38 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java @@ -5,4 +5,15 @@ * @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 String[] removeDuplicates(int maxNumberOfDuplications) { + return null; + } + + public String[] removeDuplicatesExactly(int exactNumberOfDuplicates) { + return null; + } } From 652309e3ab520dc45ebcf188ec1c776964b55c3e Mon Sep 17 00:00:00 2001 From: wes Date: Fri, 15 Nov 2019 09:53:54 -0500 Subject: [PATCH 2/4] IntegerDuplicateDeleter tests passing --- .../looplabs/IntegerDuplicateDeleter.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java index 0c22a16..a1ed4e1 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java @@ -1,5 +1,8 @@ package com.zipcodewilmington.looplabs; +import java.lang.reflect.Array; +import java.util.ArrayList; + /** * Created by leon on 1/29/18. * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class. @@ -10,10 +13,32 @@ public IntegerDuplicateDeleter(Integer[] intArray) { } public Integer[] removeDuplicates(int maxNumberOfDuplications) { - return null; + ArrayList newList = new ArrayList<>(); + for (Integer num : array) { + if (getNumberOfOccurrances(num) < maxNumberOfDuplications) { + newList.add(num); + } + } + return newList.toArray(new Integer[0]); + } + + public Integer getNumberOfOccurrances(Integer element) { + Integer count = 0; + for (Integer num : array) { + if (num.equals(element)) { + count++; + } + } + return count; } public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) { - return null; + ArrayList newList = new ArrayList<>(); + for (Integer num : array) { + if (getNumberOfOccurrances(num) != exactNumberOfDuplications) { + newList.add(num); + } + } + return newList.toArray(new Integer[0]); } } From 381b7a256779b757aae00a704e3df74e11e37c68 Mon Sep 17 00:00:00 2001 From: wes Date: Fri, 15 Nov 2019 10:11:12 -0500 Subject: [PATCH 3/4] refactor IntegerDuplicateDeleter with Arrays.stream --- .../looplabs/IntegerDuplicateDeleter.java | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java index a1ed4e1..2cad9e0 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java @@ -2,6 +2,10 @@ 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. @@ -13,32 +17,22 @@ public IntegerDuplicateDeleter(Integer[] intArray) { } public Integer[] removeDuplicates(int maxNumberOfDuplications) { - ArrayList newList = new ArrayList<>(); - for (Integer num : array) { - if (getNumberOfOccurrances(num) < maxNumberOfDuplications) { - newList.add(num); - } - } - return newList.toArray(new Integer[0]); + return Arrays.stream(array) + .filter(element -> getNumberOfOccurrances(element) < maxNumberOfDuplications) + .collect(Collectors.toList()) + .toArray(new Integer[0]); } - public Integer getNumberOfOccurrances(Integer element) { - Integer count = 0; - for (Integer num : array) { - if (num.equals(element)) { - count++; - } - } - return count; + public long getNumberOfOccurrances(Integer element) { + return (Arrays.stream(array) + .filter(num -> num == element) + .count()); } public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) { - ArrayList newList = new ArrayList<>(); - for (Integer num : array) { - if (getNumberOfOccurrances(num) != exactNumberOfDuplications) { - newList.add(num); - } - } - return newList.toArray(new Integer[0]); + return Arrays.stream(array) + .filter(element -> getNumberOfOccurrances(element) != exactNumberOfDuplications) + .collect(Collectors.toList()) + .toArray(new Integer[0]); } } From bbc23c681427b363722f98b86c2b202daf41fdc1 Mon Sep 17 00:00:00 2001 From: wes Date: Fri, 15 Nov 2019 10:14:13 -0500 Subject: [PATCH 4/4] all tests passing --- .../looplabs/StringDuplicateDeleter.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java index 90ccd38..9a16e4e 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java @@ -1,5 +1,8 @@ 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. @@ -9,11 +12,24 @@ 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 null; + + return Arrays.stream(array) + .filter(element -> getNumberOfOccurrances(element) < maxNumberOfDuplications) + .collect(Collectors.toList()) + .toArray(new String[0]); } - public String[] removeDuplicatesExactly(int exactNumberOfDuplicates) { - return null; + public String[] removeDuplicatesExactly(int exactNumberOfDuplications) { + return Arrays.stream(array) + .filter(element -> getNumberOfOccurrances(element) != exactNumberOfDuplications) + .collect(Collectors.toList()) + .toArray(new String[0]); } }