diff --git a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java index ee550c5..ee52574 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java @@ -1,8 +1,76 @@ package com.zipcodewilmington.looplabs; +import java.lang.reflect.Array; + /** * 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 { + + private Integer[] result; + + public IntegerDuplicateDeleter(Integer[] intArray) { + + super(intArray); + } + + @Override + public Integer[] removeDuplicates(int maxNumberOfDuplications) { + + int duplicates = 0; + int indexNewAray = 0; + + for(Integer index : array){ + + if(countOccurences(index) >= maxNumberOfDuplications) + duplicates++; + } + + result = new Integer[array.length - duplicates]; + + for(Integer index : array){ + + if(countOccurences(index) < maxNumberOfDuplications) { + result[indexNewAray] = index; + indexNewAray++; + } + } + + return result; + } + + @Override + public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) { + int duplicates = 0; + int indexNewAray = 0; + + for(Integer index : array){ + + if(countOccurences(index) == exactNumberOfDuplications) + duplicates++; + } + + result = new Integer[array.length - duplicates]; + + for(Integer index : array){ + + if(countOccurences(index) != exactNumberOfDuplications) { + result[indexNewAray] = index; + indexNewAray++; + } + } + + return result; + } + + public int countOccurences(int index){ + int counter = 0; + + for(Integer number : array) { + if (index == number) + counter++; + } + return counter; + } } diff --git a/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java index 4818fe3..07fdaab 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java @@ -5,4 +5,71 @@ * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class. */ public final class StringDuplicateDeleter extends DuplicateDeleter { + + private String[] result; + + public StringDuplicateDeleter(String[] intArray) { + + super(intArray); + } + + @Override + public String[] removeDuplicates(int maxNumberOfDuplications) { + + int duplicates = 0; + int indexNewAray = 0; + + for(String s : array){ + + if(countOccurences(s) >= maxNumberOfDuplications) + duplicates++; + } + + result = new String[array.length - duplicates]; + + for(String s: array){ + + if(countOccurences(s) < maxNumberOfDuplications) { + result[indexNewAray] = s; + indexNewAray++; + } + } + + return result; + } + + @Override + public String[] removeDuplicatesExactly(int exactNumberOfDuplications) { + + int duplicates = 0; + int indexNewAray = 0; + + for(String s : array){ + + if(countOccurences(s) == exactNumberOfDuplications) + duplicates++; + } + + result = new String[array.length - duplicates]; + + for(String s: array){ + + if(countOccurences(s) != exactNumberOfDuplications) { + result[indexNewAray] = s; + indexNewAray++; + } + } + + return result; + } + + public int countOccurences(String string){ + int counter = 0; + + for(String s : array) { + if (s.equals(string)) + counter++; + } + return counter; + } }