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