1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
-
- public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
- public StringDuplicateDeleter(String[] array){
- super(array);
-
- }
-
- private int countOccurrence(String target){
- int occurrences = 0;
- for(String nextOne : array){
- if(nextOne.equals(target)){
- occurrences ++;
- }
- }
- return occurrences;
- }
-
- public String[] removeDuplicatesExactly(int exactNumberOfDuplications){
- String[] newArray =new String[array.length];
- int newArrayIndex= 0;
- //go through each element in the array
- for(String nextWord : array) {
- int occurance = countOccurrence(nextWord);
- if(occurance != (exactNumberOfDuplications)) {
- newArray[newArrayIndex] = nextWord;
- newArrayIndex++;
- }
- }
- //count occurrance
- String[] finalArray = new String[newArrayIndex];
- System.arraycopy(newArray, 0, finalArray, 0, newArrayIndex);
- return finalArray;
- }
-
- public String[] removeDuplicates(int maxNumberOfDuplications){
- String[] newArray =new String[array.length];
- int newArrayIndex= 0;
- //go through each element in the array
- for(String nextWord : array) {
- int occurance = countOccurrence(nextWord);
- if(occurance < maxNumberOfDuplications) {
- newArray[newArrayIndex] = nextWord;
- newArrayIndex++;
- }
- }
- //count occurrance
- String[] finalArray = new String[newArrayIndex];
- System.arraycopy(newArray, 0, finalArray, 0, newArrayIndex);
- return finalArray;
- }
-
- }
|