diff --git a/src/main/java/com/zipcodewilmington/looplabs/DuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/DuplicateDeleter.java index c176838..d29171b 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/DuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/DuplicateDeleter.java @@ -6,10 +6,14 @@ public abstract class DuplicateDeleter implements DuplicateDeleterInterface { protected final T[] array; + public DuplicateDeleter(T[] intArray) { this.array = 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/IntegerDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java index ee550c5..b31d681 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); + } + + Integer[] myCopyOfArray = Arrays.copyOf(this.array, this.array.length); + + /** + * + * @param maxNumberOfDuplications + * @return returns an array with integers occuring less than the maxNumberOfDuplications + */ + @Override + public Integer[] removeDuplicates(int maxNumberOfDuplications) { + Integer[] outPut = new Integer[0]; + int outPutIndex = 0; + for (int s : myCopyOfArray) { + + if (getNumberOfOccurrences(myCopyOfArray, s) < maxNumberOfDuplications) { + outPut = Arrays.copyOf(outPut, outPut.length + 1); + outPut[outPutIndex] = s; + outPutIndex++; + } + } + return outPut; + } + + /** + * + * @param exactNumberOfDuplications + * @return returns an array of Integers not the exactNumberOfDuplications + */ + + @Override + public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) { + Integer[] outPut = new Integer[0]; + int outPutIndex = 0; + for (int s : myCopyOfArray) { + + if (getNumberOfOccurrences(myCopyOfArray, s) != exactNumberOfDuplications) { + outPut = Arrays.copyOf(outPut, outPut.length + 1); + outPut[outPutIndex] = s; + outPutIndex++; + } + } + + return outPut; + } + + /** + * + * @param array an array of Integers + * @param value the value to which the number of occurence in the array is to be determined + * @return the number of times a value occurs in the array + */ + public static int getNumberOfOccurrences(Integer[] array, Integer value) { + + int counter = 0; + for (int i = 0; i < array.length; i++) { + if (array[i] == value) { + counter++; + } + } + return counter; + } + +} \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java index 4818fe3..b8e67dd 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java @@ -1,8 +1,62 @@ 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[] stringArray) { + super(stringArray); + } + String[] myCopyOfArray = Arrays.copyOf(this.array,this.array.length); + + /** + * + * @param maxNumberOfDuplications + * @return + */ + @Override + public String[] removeDuplicates(int maxNumberOfDuplications) { + String[] outPut = new String[0]; + int outPutIndex = 0; + for (String s : myCopyOfArray) { + + if(getNumberOfOccurrences(myCopyOfArray, s)< maxNumberOfDuplications) { + outPut = Arrays.copyOf(outPut, outPut.length+1); + outPut[outPutIndex] = s; + outPutIndex++; + } + } + + return outPut; + } + @Override + public String[] removeDuplicatesExactly(int exactNumberOfDuplications) { + String[] outPut = new String[0]; + int outPutIndex = 0; + for (String s : myCopyOfArray) { + if (getNumberOfOccurrences(myCopyOfArray, s) != exactNumberOfDuplications) { + outPut = Arrays.copyOf(outPut, outPut.length+1); + outPut[outPutIndex] = s; + outPutIndex++; + } + + } + return outPut; + } + + public static int getNumberOfOccurrences(String[] array, String value) { + + int counter = 0; + for(int i=0;i