-
Notifications
You must be signed in to change notification settings - Fork 24
Duplicate Deleter #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Integer> { | ||
|
|
||
|
|
||
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would be less memory intensive if you figured out first how many spaces you'd need in your outputArray before inserting values. This way you don't have to create a copy every single time. |
||
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you're pulling the value from the array you are checking, then it seems like this in your program is always going to be returning true. Is that correct?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got what is going on here. This fine, but I would name the method something more like |
||
| for (Integer i : inputArray) { | ||
| if (i == value) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<String> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like this class is mostly the same as Integer deleter. My feedback is pretty much the same. |
||
| } | ||
|
|
||
|
|
||
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| counter++; | ||
| } | ||
| } | ||
| return counter; | ||
| } | ||
|
|
||
| public static boolean valueOccurs(String[] inputArray, String word) { | ||
| for (String x : inputArray) { | ||
| if (x == word) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something seems off about the names here. I don't quite get why you continue if a value occurs. On top of that you can simply use
if (valueOccurs(toDelete, i)without the== true. by default an if statement is looking for a true statement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I get whats going on now. you're checking if its a value that has already been set to be deleted. this block is just a bit confusing. Need better names for variables.