1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import java.util.Arrays;
-
- public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
- private String[] temp;
-
- public StringDuplicateDeleter(String[] array) {
- super(array);
- temp = Arrays.copyOf(array, array.length);
- }
-
-
- public String[] removeDuplicates(int maxNumberOfDuplications){
- int numberValsRemoved = 0;
- int index = 0;
-
- while (index < array.length) {
- int currValueCount = countCurrValue(index, array[index]);
- if (currValueCount >= maxNumberOfDuplications) {
- removeCurrValue(index, currValueCount);
- numberValsRemoved += currValueCount;
- }
- index += currValueCount;
- }
-
- return buildReturnArray(numberValsRemoved);
- }
-
- public String[] removeDuplicatesExactly(int exactNumberOfDuplications){
- int numberValsRemoved = 0;
- int index = 0;
- while (index < array.length) {
- int currValueCount = countCurrValue(index, array[index]);
- if (currValueCount == exactNumberOfDuplications) {
- removeCurrValue(index, currValueCount);
- numberValsRemoved += currValueCount;
- }
- index += currValueCount;
- }
- return buildReturnArray(numberValsRemoved);
- }
-
- private int countCurrValue(int index, String value) {
- int count = 0;
- while (true) {
- if (index < array.length && array[index].equals(value)) {
- count++;
- index++;
- }
- else break;
- }
- return count;
- }
-
- private void removeCurrValue(int index, int currValueCount) {
- for (int j = index + currValueCount - 1; j >= index ; j--) {
- temp[j] = null;
- }
- }
-
- private String[] buildReturnArray(int numberValsRemoved) {
- String[] newArray = new String[array.length - numberValsRemoved];
- int newArrayPos = 0;
-
- for (int i = 0; i < temp.length; i++) {
- if (temp[i] != null) {
- newArray[newArrayPos] = temp[i];
- newArrayPos++;
- }
- }
-
- temp = Arrays.copyOf(array, array.length);
- return newArray;
- }
-
-
- }
|