StringDuplicateDeleter.java 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
  2. public StringDuplicateDeleter(String[] array){
  3. super(array);
  4. }
  5. private int countOccurrence(String target){
  6. int occurrences = 0;
  7. for(String nextOne : array){
  8. if(nextOne.equals(target)){
  9. occurrences ++;
  10. }
  11. }
  12. return occurrences;
  13. }
  14. public String[] removeDuplicatesExactly(int exactNumberOfDuplications){
  15. String[] newArray =new String[array.length];
  16. int newArrayIndex= 0;
  17. //go through each element in the array
  18. for(String nextWord : array) {
  19. int occurance = countOccurrence(nextWord);
  20. if(occurance != (exactNumberOfDuplications)) {
  21. newArray[newArrayIndex] = nextWord;
  22. newArrayIndex++;
  23. }
  24. }
  25. //count occurrance
  26. String[] finalArray = new String[newArrayIndex];
  27. System.arraycopy(newArray, 0, finalArray, 0, newArrayIndex);
  28. return finalArray;
  29. }
  30. public String[] removeDuplicates(int maxNumberOfDuplications){
  31. String[] newArray =new String[array.length];
  32. int newArrayIndex= 0;
  33. //go through each element in the array
  34. for(String nextWord : array) {
  35. int occurance = countOccurrence(nextWord);
  36. if(occurance < maxNumberOfDuplications) {
  37. newArray[newArrayIndex] = nextWord;
  38. newArrayIndex++;
  39. }
  40. }
  41. //count occurrance
  42. String[] finalArray = new String[newArrayIndex];
  43. System.arraycopy(newArray, 0, finalArray, 0, newArrayIndex);
  44. return finalArray;
  45. }
  46. }