Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

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.

Copy link
Contributor

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.

} 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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 isValueInArray and order the arguments (int valueToCheckFor, Integer[] arrayToSearchThrough)

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> {
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

== is not correct for strings. Use word.equals

counter++;
}
}
return counter;
}

public static boolean valueOccurs(String[] inputArray, String word) {
for (String x : inputArray) {
if (x == word) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

== is not correct for strings. Use word.equals

return true;
}
}
return false;
}
}