Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -6,10 +6,14 @@
public abstract class DuplicateDeleter<T> implements DuplicateDeleterInterface<T> {
protected final T[] array;


public DuplicateDeleter(T[] intArray) {
this.array = intArray;
}

abstract public T[] removeDuplicates(int maxNumberOfDuplications);
{

}
abstract public T[] removeDuplicatesExactly(int exactNumberOfDuplications);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,111 @@
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);
}
Integer[] myCopyOfArray = Arrays.copyOf(this.array, this.array.length);

@Override
public Integer[] removeDuplicates(int maxNumberOfDuplications) {
Integer[] toBeRemoved = new Integer[0];
int toBeRemovedIndex = 0;
for (int s : myCopyOfArray) {
if (checkExistence(toBeRemoved, s)) {
continue;
} else if (getNumberOfOccurrences(myCopyOfArray, s) >= maxNumberOfDuplications) {
toBeRemoved = Arrays.copyOf(toBeRemoved, toBeRemoved.length + 1);
toBeRemoved[toBeRemovedIndex] = s;
toBeRemovedIndex++;
}

}
int resultIndex = 0;
Integer[] resultArray = Arrays.copyOf(myCopyOfArray, myCopyOfArray.length);

for (int r = 0; r < toBeRemoved.length; r++) {
resultArray = deleter(resultArray, toBeRemoved[r]);
}
return resultArray;
}


@Override
public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
Integer[] toBeRemoved = new Integer[0];
int toBeRemovedIndex = 0;
for (int s : myCopyOfArray) {
if (checkExistence(toBeRemoved, s)) {
continue;
} else if (getNumberOfOccurrences(myCopyOfArray, s) == exactNumberOfDuplications) {
toBeRemoved = Arrays.copyOf(toBeRemoved, toBeRemoved.length + 1);
toBeRemoved[toBeRemovedIndex] = s;
toBeRemovedIndex++;
}

}
int resultIndex = 0;
Integer[] resultArray = Arrays.copyOf(myCopyOfArray, myCopyOfArray.length);

for (int r = 0; r < toBeRemoved.length; r++) {
resultArray = deleter(resultArray, toBeRemoved[r]);
System.out.println(resultArray);
}
return resultArray;
}

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;
}
public static Integer[] deleter(Integer[] array, Integer valueToRemove) {
int counter =getNumberOfOccurrences(array, valueToRemove);
Integer[] valuesToKeep = new Integer[array.length];
for(int i=0;i<array.length;i++){
if(!valueToRemove.equals(array[i]))
{
valuesToKeep[i]=array[i];

}
}

//Resizing the valuesToKeep array by getting rid of the null values.
Integer[] resultArray = new Integer[array.length-counter];
int resultArrayIndex=0;

for(Integer element: valuesToKeep){

if(element!=null){
resultArray[resultArrayIndex]=element;
resultArrayIndex++;
}


}

return resultArray;
}

public static boolean checkExistence(Integer[] array, Integer checkElement){
for(Integer element: array){
if(checkElement.equals(element)){
return true;
}
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,105 @@
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> {

public StringDuplicateDeleter(String[] stringArray) {
super(stringArray);
}
String[] myCopyOfArray = Arrays.copyOf(this.array,this.array.length);

@Override
public String[] removeDuplicates(int maxNumberOfDuplications) {
String[] toBeRemoved = new String[0];
int toBeRemovedIndex = 0;
for (String s : myCopyOfArray) {
if (checkExistence(toBeRemoved,s)) {
continue;
} else if (getNumberOfOccurrences(myCopyOfArray, s)>= maxNumberOfDuplications) {
toBeRemoved = Arrays.copyOf(toBeRemoved, toBeRemoved.length+1);
toBeRemoved[toBeRemovedIndex] = s;
toBeRemovedIndex++;
}

}

String[] resultArray = Arrays.copyOf(myCopyOfArray, myCopyOfArray.length);
for (int r = 0; r < toBeRemoved.length; r++) {
resultArray = deleter(resultArray, toBeRemoved[r]);
}
return resultArray;
}
@Override
public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
String[] toBeRemoved = new String[0];
int toBeRemovedIndex = 0;
for (String s : myCopyOfArray) {
if (checkExistence(toBeRemoved,s)) {
continue;
} else if (getNumberOfOccurrences(myCopyOfArray, s) == exactNumberOfDuplications) {
toBeRemoved = Arrays.copyOf(toBeRemoved, toBeRemoved.length+1);
toBeRemoved[toBeRemovedIndex] = s;
toBeRemovedIndex++;
}

}

String[] resultArray = Arrays.copyOf(myCopyOfArray, myCopyOfArray.length);
for (int r = 0; r < toBeRemoved.length; r++) {
resultArray= deleter(resultArray, toBeRemoved[r]);
}
return resultArray;
}

public static int getNumberOfOccurrences(String[] array, String value) {

int counter = 0;
for(int i=0;i<array.length;i++){
if(array[i].equalsIgnoreCase(value)){
counter++;
}
}
return counter;
}
public static String[] deleter(String[] array, String valueToRemove) {
int counter =getNumberOfOccurrences(array, valueToRemove);
String[] valuesToKeep = new String[array.length];
for(int i=0;i<array.length;i++){
if(!(array[i].equals(valueToRemove)))
{
valuesToKeep[i]=array[i];

}
}
String[] resultArray = new String[array.length-counter];
int resultArrayIndex=0;

for( String element: valuesToKeep){

if(element!=null){
resultArray[resultArrayIndex]=element;
resultArrayIndex++;
}


}

return resultArray;
}

public static boolean checkExistence(String[] array, String checkElement){
for(int a=0;a<array.length;a++){
if(checkElement.equals(array[a])){
return true;
}
}
return false;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Created by leon on 1/25/18.
* @ATTENTION_TO_STUDENTS You are forbidden from modifying this class.
*/
*/
public class IntegerDuplicateDeleterTest {

private static Integer[] intArray;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void setup() {
@Test
public void testRemoveDuplicatesExactly1() {
String[] expected = new String[]{"aba", "aba", "bba", "bba", "bba", "bba", "bbb", "bbb"};
// "aba", "aba", "bba", "bba", "bba", "bba", "bbb", "bbb"
String[] actual = deleter.removeDuplicatesExactly(1);
TestUtils.assertArrayEquality(expected, actual);
}
Expand Down