1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import java.util.Arrays;
  2. public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
  3. private Integer[] temp;
  4. public IntegerDuplicateDeleter(Integer[] array) {
  5. super(array);
  6. temp = Arrays.copyOf(array, array.length);
  7. }
  8. public Integer[] removeDuplicates(int maxNumberOfDuplications){
  9. int numberValsRemoved = 0;
  10. int index = 0;
  11. while (index < array.length) {
  12. int currValueCount = countCurrValue(index, array[index]);
  13. if (currValueCount >= maxNumberOfDuplications) {
  14. removeCurrValue(index, currValueCount);
  15. numberValsRemoved += currValueCount;
  16. }
  17. index += currValueCount;
  18. }
  19. return buildReturnArray(numberValsRemoved);
  20. }
  21. public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications){
  22. int numberValsRemoved = 0;
  23. int index = 0;
  24. while (index < array.length) {
  25. int currValueCount = countCurrValue(index, array[index]);
  26. if (currValueCount == exactNumberOfDuplications) {
  27. removeCurrValue(index, currValueCount);
  28. numberValsRemoved += currValueCount;
  29. }
  30. index += currValueCount;
  31. }
  32. return buildReturnArray(numberValsRemoved);
  33. }
  34. private int countCurrValue(int index, Integer value) {
  35. int count = 0;
  36. while (true) {
  37. if (index < array.length && array[index] == value) {
  38. count++;
  39. index++;
  40. }
  41. else break;
  42. }
  43. return count;
  44. }
  45. private void removeCurrValue(int index, int currValueCount) {
  46. for (int j = index + currValueCount - 1; j >= index ; j--) {
  47. temp[j] = null;
  48. }
  49. }
  50. private Integer[] buildReturnArray(int numberValsRemoved) {
  51. Integer[] newArray = new Integer[array.length - numberValsRemoved];
  52. int newArrayPos = 0;
  53. for (int i = 0; i < temp.length; i++) {
  54. if (temp[i] != null) {
  55. newArray[newArrayPos] = temp[i];
  56. newArrayPos++;
  57. }
  58. }
  59. temp = Arrays.copyOf(array, array.length);
  60. return newArray;
  61. }
  62. }