StringDuplicateDeleter.java 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
  2. public StringDuplicateDeleter(String[] array){
  3. super(array);
  4. }
  5. public String[] removeDuplicates(int maxNumberOfDuplications){
  6. int occurence = 0;
  7. boolean flag = true;
  8. String[] revisedArr = this.array;
  9. while(flag){
  10. flag = false;
  11. for (int i = 0; i < revisedArr.length; i++){
  12. for (int j = 0; j < revisedArr.length; j++){
  13. if (revisedArr[i].equals(revisedArr[j])){
  14. occurence++;
  15. }
  16. }
  17. if (occurence >= maxNumberOfDuplications){
  18. revisedArr = removeValue(revisedArr, revisedArr[i], occurence);
  19. flag = true;
  20. }
  21. occurence = 0;
  22. }
  23. }
  24. return revisedArr;
  25. }
  26. public String[] removeDuplicatesExactly(int exactNumberOfDuplications){
  27. int occurence = 0;
  28. boolean flag = true;
  29. String[] revisedArr = this.array;
  30. while(flag){
  31. flag = false;
  32. for (int i = 0; i < revisedArr.length; i++){
  33. for (int j = 0; j < revisedArr.length; j++){
  34. if (revisedArr[i].equals(revisedArr[j])){
  35. occurence++;
  36. }
  37. }
  38. if (occurence == exactNumberOfDuplications){
  39. revisedArr = removeValue(revisedArr, revisedArr[i], occurence);
  40. flag = true;
  41. }
  42. occurence = 0;
  43. }
  44. }
  45. return revisedArr;
  46. }
  47. public String[] removeValue(String[] array, String valueToRemove, int occurences) {
  48. int howManyToDelete = occurences;
  49. String[] revisedArray = new String[array.length - howManyToDelete];
  50. int index = 0;
  51. for(String element: array){
  52. if(element != valueToRemove){
  53. revisedArray[index] = element;
  54. index++;
  55. }
  56. }
  57. return revisedArray;
  58. }
  59. }