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