IntegerDuplicateDeleter.java 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import java.util.Arrays;
  2. public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
  3. public IntegerDuplicateDeleter(Integer[] array){
  4. super(array);
  5. }
  6. public Integer[] removeDuplicates(int maxNumberOfDuplications){
  7. int occurence = 0;
  8. boolean flag = true;
  9. Integer[] revisedArr = this.array;
  10. while(flag){
  11. flag = false;
  12. for (int i = 0; i < revisedArr.length; i++){
  13. for (int j = 0; j < revisedArr.length; j++){
  14. if (revisedArr[i].equals(revisedArr[j])){
  15. occurence++;
  16. }
  17. }
  18. if (occurence >= maxNumberOfDuplications){
  19. revisedArr = removeValue(revisedArr, revisedArr[i], occurence);
  20. flag = true;
  21. }
  22. occurence = 0;
  23. }
  24. }
  25. return revisedArr;
  26. }
  27. public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications){
  28. int occurence = 0;
  29. boolean flag = true;
  30. Integer[] revisedArr = this.array;
  31. while(flag){
  32. flag = false;
  33. for (int i = 0; i < revisedArr.length; i++){
  34. for (int j = 0; j < revisedArr.length; j++){
  35. if (revisedArr[i].equals(revisedArr[j])){
  36. occurence++;
  37. }
  38. }
  39. if (occurence == exactNumberOfDuplications){
  40. revisedArr = removeValue(revisedArr, revisedArr[i], occurence);
  41. flag = true;
  42. }
  43. occurence = 0;
  44. }
  45. }
  46. return revisedArr;
  47. }
  48. public Integer[] removeValue(Integer[] array, Integer valueToRemove, int occurences) {
  49. int howManyToDelete = occurences;
  50. Integer[] revisedArray = new Integer[array.length - howManyToDelete];
  51. int index = 0;
  52. for(Integer element: array){
  53. if(element != valueToRemove){
  54. revisedArray[index] = element;
  55. index++;
  56. }
  57. }
  58. return revisedArray;
  59. }
  60. }