IntegerDuplicateDeleter.java 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
  2. StringBuilder valuesToKeep = new StringBuilder();
  3. public IntegerDuplicateDeleter(Integer[] array){
  4. super(array);
  5. }
  6. public Integer[] removeDuplicates(int maxNumberOfDuplications){
  7. resetStringBuilder();
  8. for(int i=0; i<=array.length-1; i++){
  9. //if the number of occurrences is less than the max number of duplicates
  10. if(getNumberOfOccurrences(array[i]) < maxNumberOfDuplications){
  11. //then save the value to values to remove array and increase total dups by 1
  12. setValuesToKeep(String.valueOf(array[i]));
  13. }
  14. }
  15. return convertStringArrToIntegerArr();
  16. }
  17. public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications){
  18. resetStringBuilder();
  19. for(int i=0; i<=array.length-1; i++){
  20. if(getNumberOfOccurrences(array[i]) != exactNumberOfDuplications){
  21. setValuesToKeep(String.valueOf(array[i]));
  22. }
  23. }
  24. return convertStringArrToIntegerArr();
  25. }
  26. public int getNumberOfOccurrences(int value){
  27. int numberOfOccurrences = 0;
  28. for (int element : array){
  29. if(element ==value){
  30. numberOfOccurrences++;
  31. }
  32. }
  33. return numberOfOccurrences;
  34. }
  35. public void resetStringBuilder(){
  36. valuesToKeep.delete(0,valuesToKeep.length());
  37. }
  38. public void setValuesToKeep(String value){
  39. valuesToKeep.append(value + ",");
  40. }
  41. public String[] getValuesToKeep(){
  42. String tempString = valuesToKeep.toString();
  43. String[] valuesToKeepArray = {};
  44. if(tempString.length()<1){
  45. return valuesToKeepArray;
  46. }
  47. valuesToKeepArray = tempString.split(",");
  48. return valuesToKeepArray;
  49. }
  50. public Integer[] convertStringArrToIntegerArr(){
  51. String[] stringValuesToKeepArray = getValuesToKeep();
  52. Integer[] valuesToKeepArray = new Integer [stringValuesToKeepArray.length];
  53. for (int i=0; i<stringValuesToKeepArray.length; i++){
  54. valuesToKeepArray[i] = Integer.parseInt(stringValuesToKeepArray[i]);
  55. }
  56. return valuesToKeepArray;
  57. }
  58. }