12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
-
-
- public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
-
- public StringDuplicateDeleter(String [] array) {
- super(array);
- }
-
- public String[] removeDuplicates(int maxNumberOfDuplications) {
- String[] removed = new String[array.length];
- int removedIndex = 0;
- for (String string: array) {
- int occurence = countOccurance(string);
- if (occurence < maxNumberOfDuplications) {
- removed[removedIndex] = string;
- removedIndex ++;
- }
- }
- String[] finalArray = new String[removedIndex];
- System.arraycopy(removed, 0, finalArray, 0, removedIndex);
- return finalArray;
- }
-
- public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
- String[] newArray = new String[array.length];
-
- int newArrayIndex = 0;
-
- for (String string : array) {
- int occurence = countOccurance(string);
- if (occurence != exactNumberOfDuplications) {
- newArray[newArrayIndex] =string;
- newArrayIndex++;
- }
- }
-
- String[] finalArray = new String[newArrayIndex];
- System.arraycopy(newArray, 0, finalArray, 0, newArrayIndex);
- return finalArray;
- }
-
- private int countOccurance(String string) {
- int count = 0;
- for (String i : array) {
- if (string.equals(i)) {
- count = count + 1;
- }
- }
- return count;
- }
- }
|