IntegerDuplicateDeleter.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
  2. public IntegerDuplicateDeleter(Integer[] array) {
  3. super(array);
  4. }
  5. public Integer[] removeDuplicates(int maxNumberOfDuplications) {
  6. StringBuilder sb = new StringBuilder();
  7. for (int i = 0; i < array.length; i++) {
  8. if(getCount(array[i]) < maxNumberOfDuplications){
  9. sb.append((array[i]) + " ");
  10. }
  11. }
  12. String arrString = sb.toString();
  13. String [] arrPlus = arrString.split(" ");
  14. Integer[] newArr = new Integer[arrPlus.length];
  15. for (int i = 0; i < newArr.length; i++) {
  16. try {newArr[i] = Integer.parseInt(arrPlus[i]);}
  17. catch (NumberFormatException e){}
  18. }
  19. Integer[] empty = new Integer[]{};
  20. if (newArr.length == 1 && newArr[0] == null) {
  21. return empty;
  22. } else { return newArr;}
  23. }
  24. public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
  25. StringBuilder sb = new StringBuilder();
  26. String tester = array.toString();
  27. for (int i = 0; i < array.length; i++) {
  28. if(getCount(array[i]) < exactNumberOfDuplications || getCount(array[i])
  29. > exactNumberOfDuplications) {
  30. sb.append((array[i]) + " ");
  31. }
  32. }
  33. String arrString = sb.toString();
  34. String [] arrPlus = arrString.split(" ");
  35. Integer[] newArr = new Integer[arrPlus.length];
  36. for (int i = 0; i < newArr.length; i++) {
  37. try {newArr[i] = Integer.parseInt(arrPlus[i]);}
  38. catch (NumberFormatException e){}
  39. }
  40. Integer[] empty = new Integer[]{};
  41. if (newArr.length == 1 && newArr[0] == null) {
  42. return empty;
  43. } else { return newArr;}
  44. }
  45. private int getCount (Integer value){
  46. int count = 0;
  47. for (Integer e : array){
  48. if (e == value) {
  49. count++;
  50. }
  51. }
  52. return count;
  53. }
  54. }