From 504c0eb9826395bc955d834a919b02a87ad70e0d Mon Sep 17 00:00:00 2001 From: Eric Cordell Date: Sun, 11 Feb 2018 19:15:17 -0500 Subject: [PATCH] Duplicate Deleter --- .../looplabs/IntegerDuplicateDeleter.java | 87 +++++++++++++++++++ .../looplabs/StringDuplicateDeleter.java | 86 +++++++++++++++++- 2 files changed, 172 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java index ee550c5..3a645f8 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java @@ -1,8 +1,95 @@ 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); + } + + public Integer[] removeDuplicates(int maxNumberOfDuplications) { + Integer[] outputArray = this.array; + Integer[] toDelete = new Integer[0]; + + int deleteCounter = 0; + for (Integer x : this.array) { + if (valueOccurs(toDelete, x) == true) { + continue; + + } else if (timesValueOccurs(this.array, x) >= maxNumberOfDuplications) { + toDelete = Arrays.copyOf(toDelete, toDelete.length + 1); + toDelete[deleteCounter] = x; + deleteCounter++; + } + + for (Integer y : toDelete) { + outputArray = remove(outputArray, y); + } + } + System.out.println(outputArray); + return outputArray; + } + + //@Override + public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) { + Integer[] outputArray = this.array; + Integer[] toDelete = new Integer[0]; + int deleteCounter = 0; + for (Integer i : this.array) { + if (valueOccurs(toDelete, i) == true) { + continue; + } else if (timesValueOccurs(this.array, i) == exactNumberOfDuplications) { + toDelete = Arrays.copyOf(toDelete, toDelete.length + 1); + toDelete[deleteCounter] = i; + deleteCounter++; + } + } + for (Integer i : toDelete) { + outputArray = remove(outputArray, i); + } + + return outputArray; + } + + + public static Integer[] remove(Integer[] inputArray, int value) { + Integer[] revisedArray = inputArray; + Integer[] outputArray = new Integer[0]; + int counter = 0; + for (int i = 0; i < revisedArray.length; i++) { + if (!revisedArray[i].equals(value)) { + outputArray = Arrays.copyOf(outputArray, outputArray.length + 1); + outputArray[counter] = revisedArray[i]; + counter++; + } + } + return outputArray; + } + + public static int timesValueOccurs(Integer[] inputArray, int value) { + int counter = 0; + for (Integer i : inputArray) { + if (i == value) { + counter++; + } + } + return counter; + } + + public static boolean valueOccurs(Integer[] inputArray, int value) { + for (Integer i : inputArray) { + if (i == value) { + return true; + } + } + return false; + } + } diff --git a/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java b/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java index 4818fe3..149f518 100644 --- a/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java +++ b/src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java @@ -1,8 +1,92 @@ 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); + } + + public String[] removeDuplicates (int maxNumberOfDuplications){ + String[] outputArray = this.array; + String [] toDelete = new String[0]; + + int deleteCounter = 0; + for(String x : this.array){ + if (valueOccurs(toDelete, x) == true){ + continue; + } + else if (timesValueOccurs(this.array, x) >= maxNumberOfDuplications){ + toDelete = Arrays.copyOf(toDelete, toDelete.length+1); + toDelete[deleteCounter] = x; + deleteCounter++; + } + } + for(String x : toDelete){ + outputArray = remove(outputArray, x); + } + + return outputArray; + + } + + //@Override + public String[] removeDuplicatesExactly(int exactNumberOfDuplications){ + String[] outputArray = this.array; + String [] toDelete = new String[0]; + int deleteCounter = 0; + for(String x : this.array){ + if (valueOccurs(toDelete, x) == true){ + continue; + } + else if (timesValueOccurs(this.array, x) == exactNumberOfDuplications){ + toDelete = Arrays.copyOf(toDelete, toDelete.length + 1); + toDelete[deleteCounter] = x; + deleteCounter++; + } + } + for(String s : toDelete){ + outputArray = remove(outputArray, s); + } + + return outputArray; + } + + public static String[] remove(String[] inputArray, String value) { + String[] revisedArray = inputArray; + String[] outputArray = new String[0]; + int counter = 0; + for (int i = 0; i < revisedArray.length; i++) { + if (!revisedArray[i].equals(value)) { + outputArray = Arrays.copyOf(outputArray, outputArray.length + 1); + outputArray[counter] = revisedArray[i]; + counter++; + } + } + return outputArray; + } + + public static int timesValueOccurs(String[] inputArray, String word) { + int counter = 0; + for (String x : inputArray) { + if (x == word) { + counter++; + } + } + return counter; + } + + public static boolean valueOccurs(String[] inputArray, String word) { + for (String x : inputArray) { + if (x == word) { + return true; + } + } + return false; + } +} \ No newline at end of file