IntegerDuplicateDeleter.java 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
  2. public IntegerDuplicateDeleter(Integer[] array) {
  3. super(array);
  4. }
  5. public Integer[] removeDuplicates(int maxNumberOfDuplications) {
  6. Integer[] newArray = new Integer[array.length];
  7. int newArrayIndex = 0;
  8. for (Integer num : array) {
  9. int occurence = countOccurance(num);
  10. if (occurence < maxNumberOfDuplications) {
  11. newArray[newArrayIndex] = num;
  12. newArrayIndex++;
  13. }
  14. }
  15. Integer[] finalArray = new Integer[newArrayIndex];
  16. System.arraycopy(newArray, 0, finalArray, 0, newArrayIndex);
  17. return finalArray;
  18. }
  19. public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
  20. Integer[] newArray = new Integer[array.length];
  21. int newArrayIndex = 0;
  22. for (Integer num : array) {
  23. int occurence = countOccurance(num);
  24. if (occurence != exactNumberOfDuplications) {
  25. newArray[newArrayIndex] = num;
  26. newArrayIndex++;
  27. }
  28. }
  29. Integer[] finalArray = new Integer[newArrayIndex];
  30. System.arraycopy(newArray, 0, finalArray, 0, newArrayIndex);
  31. return finalArray;
  32. }
  33. private int countOccurance(Integer number) {
  34. int count = 0;
  35. for (Integer i : array) {
  36. if (number == i) {
  37. count = count + 1;
  38. }
  39. }
  40. return count;
  41. }
  42. }