123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import java.lang.StringBuilder;
-
- public class StringDuplicateDeleter extends DuplicateDeleter<String>{
- StringBuilder valuesToKeep = new StringBuilder();
-
- public StringDuplicateDeleter(String[] array){
- super(array);
- }
-
- public String[] removeDuplicates(int maxNumberOfDuplications){
- valuesToKeep.delete(0,valuesToKeep.length());
- //for each element in the array
- for(int i=0; i<=array.length-1; i++){
- //if the number of occurrences is less than the max number of duplicates
- if(getNumberOfOccurrences(array[i]) < maxNumberOfDuplications){
- //then save the value to values to remove array and increase total dups by 1
- setValuesToKeep(array[i]);
- }
- }
- String[] valuesToKeepArray = getValuesToKeep();
- return valuesToKeepArray;
- }
-
- public String[] removeDuplicatesExactly(int exactNumberOfDuplications){
- valuesToKeep.delete(0,valuesToKeep.length());
- for(int i=0; i<=array.length-1; i++){
- //if the number of occurrences is less than the max number of duplicates
- if(getNumberOfOccurrences(array[i]) != exactNumberOfDuplications){
- //then save the value to values to remove array and increase total dups by 1
- setValuesToKeep(array[i]);
- }
- }
- String[] valuesToKeepArray = getValuesToKeep();
- return valuesToKeepArray;
- }
-
- public int getNumberOfOccurrences(String value){
- int numberOfOccurrences = 0;
- for (String element : array){
- if(element.equals(value)){
- numberOfOccurrences++;
- }
- }
- return numberOfOccurrences;
- }
-
- public void setValuesToKeep(String value){
- valuesToKeep.append(value + ",");
- }
-
- public String[] getValuesToKeep(){
- String tempString = valuesToKeep.toString();
- String[] valuesToKeepArray = {};
- if(tempString.length()>1){
- valuesToKeepArray = tempString.split(",");
- }
- return valuesToKeepArray;
- }
-
- }
|