diff --git a/src/main/java/com/zipcodewilmington/looplabs/DuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/DuplicateDeleter.java index c176838..7a9b3c5 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/DuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/DuplicateDeleter.java @@ -11,5 +11,6 @@ public DuplicateDeleter(T[] intArray) { } abstract public T[] removeDuplicates(int maxNumberOfDuplications); + abstract public T[] removeDuplicatesExactly(int exactNumberOfDuplications); } \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/looplabs/DuplicateDeleterInterface.java b/src/main/java/com/zipcodewilmington/looplabs/DuplicateDeleterInterface.java index 058f8f9..bd492d9 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/DuplicateDeleterInterface.java +++ b/src/main/java/com/zipcodewilmington/looplabs/DuplicateDeleterInterface.java @@ -2,9 +2,11 @@ /** * Created by leon on 1/28/18. + * * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class. */ public interface DuplicateDeleterInterface { T[] removeDuplicates(int maxNumberOfDuplications); + T[] removeDuplicatesExactly(int exactNumberOfDuplications); } \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java index ee550c5..f45a4a7 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java @@ -1,8 +1,77 @@ package com.zipcodewilmington.looplabs; +import java.util.Arrays; + /** * 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[] removeDuplicatesExactly(int exactNumberOfDuplications) { + + Integer[] finalArray = new Integer[0]; + + for (int i = 0; i < this.array.length; i++) { + + if (getNumberOfOccurrences(this.array, this.array[i]) != exactNumberOfDuplications) { + + int actualElements = finalArray.length; + finalArray = Arrays.copyOf(finalArray, finalArray.length + 1); + finalArray[actualElements] = this.array[i]; + + } + + } + + return finalArray; + } + + public static int getNumberOfOccurrences(Integer[] inputArray, Integer value) { + + int valueCounter = 0; + + for (int arrayElement : inputArray) { + + if (arrayElement == value) { + + valueCounter++; + } + + } + + return valueCounter; + } + + + @Override + public Integer[] removeDuplicates(int maxNumberOfDuplications) { + Integer[] actualArray = new Integer[0]; + + for (int i = 0; i < this.array.length; i++) { + + if (getNumberOfOccurrences(this.array, this.array[i]) < maxNumberOfDuplications) { + + int arrayElements = actualArray.length; + actualArray = Arrays.copyOf(actualArray, actualArray.length + 1); + actualArray[arrayElements] = this.array[i]; + + } + + } + + return actualArray; + + } + } + + diff --git a/src/main/java/com/zipcodewilmington/looplabs/RandomNumberFactory.java b/src/main/java/com/zipcodewilmington/looplabs/RandomNumberFactory.java index 984cff6..24fa3e1 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/RandomNumberFactory.java +++ b/src/main/java/com/zipcodewilmington/looplabs/RandomNumberFactory.java @@ -11,6 +11,7 @@ public final class RandomNumberFactory { private RandomNumberFactory() { } + /** * @param min * @param max @@ -45,7 +46,7 @@ public static Integer createInteger(Integer min, Integer max) { */ public static Integer[] createIntegers(int min, int max, int length) { Integer[] integers = new Integer[length]; - for(int i=0; i { + + public StringDuplicateDeleter(String[] intArray) { + super(intArray); + } + + + @Override + public String[] removeDuplicatesExactly(int exactNumberOfDuplications) { + + String[] noStringDupsArray = new String[0]; + + for (int i = 0; i < this.array.length; i++) { + + if (getNumberOfOccurrences(this.array, this.array[i]) != exactNumberOfDuplications) { + + int stringElements = noStringDupsArray.length; + noStringDupsArray = Arrays.copyOf(noStringDupsArray, noStringDupsArray.length + 1); + noStringDupsArray[stringElements] = this.array[i]; + + } + + } + + return noStringDupsArray; + } + + public static int getNumberOfOccurrences(String[] inputArray, String value) { + + int valueCounter = 0; + + for (String arrayElement : inputArray) { + + if (arrayElement.equalsIgnoreCase(value)) { + + valueCounter++; + } + + } + + return valueCounter; + } + + + @Override + public String[] removeDuplicates(int maxNumberOfDuplications) { + String[] maxDupsArray = new String[0]; + + for (int i = 0; i < this.array.length; i++) { + + if (getNumberOfOccurrences(this.array, this.array[i]) < maxNumberOfDuplications) { + + int arrayElements = maxDupsArray.length; + maxDupsArray = Arrays.copyOf(maxDupsArray, maxDupsArray.length + 1); + maxDupsArray[arrayElements] = this.array[i]; + + } + + } + + return maxDupsArray; + } } diff --git a/src/test/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleterTest.java b/src/test/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleterTest.java index 3388c27..d83ae3e 100644 --- a/src/test/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleterTest.java +++ b/src/test/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleterTest.java @@ -7,6 +7,7 @@ /** * Created by leon on 1/25/18. + * * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class. */ public class IntegerDuplicateDeleterTest { @@ -37,7 +38,6 @@ public void testRemoveDuplicatesExactly1() { } - @Test public void testRemoveDuplicatesExactly2() { Integer[] expected = new Integer[]{0, 0, 0, 2, 2, 4, 4, 5, 5, 5, 9, 9, 9}; @@ -58,31 +58,6 @@ public void testRemoveDuplicatesExactly3() { } - - - - - - - - - - - - - - - - - - - - - - - - - @Test public void testRemoveDuplicates0() { Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5}; @@ -117,7 +92,7 @@ public void testRemoveDuplicates2() { public void testRemoveDuplicates3() { Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5}; DuplicateDeleter deleter = new IntegerDuplicateDeleter(array); - Integer[] expected = new Integer[]{1,1,2,4,4}; + Integer[] expected = new Integer[]{1, 1, 2, 4, 4}; Integer[] actual = deleter.removeDuplicates(3); TestUtils.assertArrayEquality(expected, actual); } @@ -127,28 +102,15 @@ public void testRemoveDuplicates3() { public void testRemoveDuplicates4() { Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5}; DuplicateDeleter deleter = new IntegerDuplicateDeleter(array); - Integer[] expected = new Integer[]{0,0,0,1,1,2,3,3,3,4,4}; + Integer[] expected = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4}; Integer[] actual = deleter.removeDuplicates(4); TestUtils.assertArrayEquality(expected, actual); } - - - - - - - - - - - - - @Test public void testRemoveDuplicatesExactlyIdempotence() { - Integer[] input = RandomNumberFactory.createIntegers(0,50,150); + Integer[] input = RandomNumberFactory.createIntegers(0, 50, 150); DuplicateDeleter deleter = new IntegerDuplicateDeleter(input); Integer[] expected = deleter.removeDuplicatesExactly(5); @@ -166,7 +128,7 @@ public void testRemoveDuplicatesExactlyIdempotence() { @Test public void testRemoveDuplicatesIdempotence() { - Integer[] input = RandomNumberFactory.createIntegers(0,50,150); + Integer[] input = RandomNumberFactory.createIntegers(0, 50, 150); DuplicateDeleter deleter = new IntegerDuplicateDeleter(input); Integer[] expected = deleter.removeDuplicates(5); diff --git a/src/test/java/com/zipcodewilmington/looplabs/StringDuplicateDeleterTest.java b/src/test/java/com/zipcodewilmington/looplabs/StringDuplicateDeleterTest.java index e9225cf..8a75fe4 100644 --- a/src/test/java/com/zipcodewilmington/looplabs/StringDuplicateDeleterTest.java +++ b/src/test/java/com/zipcodewilmington/looplabs/StringDuplicateDeleterTest.java @@ -8,6 +8,7 @@ /** * Created by leon on 1/28/18. + * * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class. */ public class StringDuplicateDeleterTest { @@ -63,18 +64,6 @@ public void testRemoveDuplicatesExactly5() { } - - - - - - - - - - - - @Test public void testRemoveDuplicates0() { String[] input = new String[]{"aa", "aa", "aa", "aa", "aa", "ab", "ba", "ba", "ba", "ba", "bb", "bb", "bb", "bb", "bb"}; @@ -85,7 +74,6 @@ public void testRemoveDuplicates0() { } - @Test public void testRemoveDuplicates1() { String[] input = new String[]{"aa", "aa", "aa", "aa", "aa", "ab", "ba", "ba", "ba", "ba", "bb", "bb", "bb", "bb", "bb"}; @@ -115,24 +103,6 @@ public void testRemoveDuplicates3() { } - - - - - - - - - - - - - - - - - - @Test public void testRemoveDuplicatesExactlyIdempotence() { String[] input = RandomNumberFactory.createStrings('a', 'c', 2, 15); diff --git a/src/test/java/com/zipcodewilmington/looplabs/TestSuite.java b/src/test/java/com/zipcodewilmington/looplabs/TestSuite.java index b519741..8bc98f8 100644 --- a/src/test/java/com/zipcodewilmington/looplabs/TestSuite.java +++ b/src/test/java/com/zipcodewilmington/looplabs/TestSuite.java @@ -5,6 +5,7 @@ /** * Created by leon on 1/28/18. + * * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class. */ @RunWith(Suite.class) diff --git a/src/test/java/com/zipcodewilmington/looplabs/TestUtils.java b/src/test/java/com/zipcodewilmington/looplabs/TestUtils.java index 1c07809..14d952a 100644 --- a/src/test/java/com/zipcodewilmington/looplabs/TestUtils.java +++ b/src/test/java/com/zipcodewilmington/looplabs/TestUtils.java @@ -6,6 +6,7 @@ /** * Created by leon on 1/28/18. + * * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class. */ public class TestUtils {