1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
-
- import java.util.Arrays;
-
- public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
- private Integer[] temp;
- public IntegerDuplicateDeleter(Integer[] array) {
- super(array);
- temp = Arrays.copyOf(array, array.length);
- }
-
- public Integer[] 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 Integer[] 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, Integer value) {
- int count = 0;
- while (true) {
- if (index < array.length && array[index] == 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 Integer[] buildReturnArray(int numberValsRemoved) {
- Integer[] newArray = new Integer[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;
- }
- }
|