StringDuplicateDeleter.java 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
  2. public StringDuplicateDeleter(String [] array) {
  3. super(array);
  4. }
  5. public String[] removeDuplicates(int maxNumberOfDuplications) {
  6. String[] removed = new String[array.length];
  7. int removedIndex = 0;
  8. for (String string: array) {
  9. int occurence = countOccurance(string);
  10. if (occurence < maxNumberOfDuplications) {
  11. removed[removedIndex] = string;
  12. removedIndex ++;
  13. }
  14. }
  15. String[] finalArray = new String[removedIndex];
  16. System.arraycopy(removed, 0, finalArray, 0, removedIndex);
  17. return finalArray;
  18. }
  19. public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
  20. String[] newArray = new String[array.length];
  21. int newArrayIndex = 0;
  22. for (String string : array) {
  23. int occurence = countOccurance(string);
  24. if (occurence != exactNumberOfDuplications) {
  25. newArray[newArrayIndex] =string;
  26. newArrayIndex++;
  27. }
  28. }
  29. String[] finalArray = new String[newArrayIndex];
  30. System.arraycopy(newArray, 0, finalArray, 0, newArrayIndex);
  31. return finalArray;
  32. }
  33. private int countOccurance(String string) {
  34. int count = 0;
  35. for (String i : array) {
  36. if (string.equals(i)) {
  37. count = count + 1;
  38. }
  39. }
  40. return count;
  41. }
  42. }