1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import java.util.Arrays;
-
- public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
- public IntegerDuplicateDeleter(Integer[] array){
- super(array);
- }
-
- public Integer[] removeDuplicates(int maxNumberOfDuplications){
- int occurence = 0;
- boolean flag = true;
- Integer[] 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 Integer[] removeDuplicatesExactly(int exactNumberOfDuplications){
- int occurence = 0;
- boolean flag = true;
- Integer[] 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 Integer[] removeValue(Integer[] array, Integer valueToRemove, int occurences) {
- int howManyToDelete = occurences;
- Integer[] revisedArray = new Integer[array.length - howManyToDelete];
- int index = 0;
- for(Integer element: array){
- if(element != valueToRemove){
- revisedArray[index] = element;
- index++;
- }
- }
- return revisedArray;
- }
- }
|