123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
-
- public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
-
- public StringDuplicateDeleter(String[] array){
- super(array);
- }
-
- public String[] removeDuplicates(int maxNumberOfDuplications){
- int occurence = 0;
- boolean flag = true;
- String[] revisedArr = this.array;
- while(flag){
- flag = false;
- for (int i = 0; i < revisedArr.length; i++){
- for (int j = 0; j < revisedArr.length; j++){
- if (revisedArr[i].equals(revisedArr[j])){
- occurence++;
- }
- }
- if (occurence >= maxNumberOfDuplications){
- revisedArr = removeValue(revisedArr, revisedArr[i], occurence);
- flag = true;
- }
- occurence = 0;
- }
- }
- return revisedArr;
- }
-
- public String[] removeDuplicatesExactly(int exactNumberOfDuplications){
- int occurence = 0;
- boolean flag = true;
- String[] revisedArr = this.array;
- while(flag){
- flag = false;
- for (int i = 0; i < revisedArr.length; i++){
- for (int j = 0; j < revisedArr.length; j++){
- if (revisedArr[i].equals(revisedArr[j])){
- occurence++;
- }
- }
- if (occurence == exactNumberOfDuplications){
- revisedArr = removeValue(revisedArr, revisedArr[i], occurence);
- flag = true;
- }
- occurence = 0;
- }
- }
- return revisedArr;
- }
-
- public String[] removeValue(String[] array, String valueToRemove, int occurences) {
- int howManyToDelete = occurences;
- String[] revisedArray = new String[array.length - howManyToDelete];
- int index = 0;
- for(String element: array){
- if(element != valueToRemove){
- revisedArray[index] = element;
- index++;
- }
- }
- return revisedArray;
- }
-
- }
|