IntegerDuplicateDeleter.java 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import java.util.*;
  2. public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
  3. public IntegerDuplicateDeleter(Integer[] array){
  4. super(array);
  5. }
  6. public Integer[] removeDuplicates(int maxNumberOfDuplications){
  7. Integer[] answer = new Integer[array.length];
  8. int answerIndex = 0;
  9. for(int i = 0; i < array.length; i++){
  10. Integer ocurrances = getAppearances(array[i]);
  11. if(ocurrances < maxNumberOfDuplications) {
  12. answer[answerIndex] = array[i];
  13. answerIndex++;
  14. }
  15. }
  16. Integer[] realAnswer = Arrays.copyOf(answer, answerIndex);
  17. return realAnswer;
  18. }
  19. public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications){
  20. Integer[] answer = new Integer[array.length];
  21. int answerIndex = 0;
  22. for(int i = 0; i < array.length; i++){
  23. Integer ocurrances = getAppearances(array[i]);
  24. if(ocurrances < exactNumberOfDuplications || ocurrances > exactNumberOfDuplications) {
  25. answer[answerIndex] = array[i];
  26. answerIndex++;
  27. }
  28. }
  29. Integer[] realAnswer = Arrays.copyOf(answer, answerIndex);
  30. return realAnswer;
  31. }
  32. public int getAppearances(int x){
  33. int counter = 0;
  34. for(int element : array ){
  35. if(element == x){
  36. counter++;
  37. }
  38. }
  39. return counter;
  40. }
  41. }