1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
-
- public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
- StringBuilder valuesToKeep = new StringBuilder();
-
- public IntegerDuplicateDeleter(Integer[] array){
- super(array);
- }
-
- public Integer[] removeDuplicates(int maxNumberOfDuplications){
- resetStringBuilder();
- 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(String.valueOf(array[i]));
- }
- }
- return convertStringArrToIntegerArr();
- }
-
- public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications){
- resetStringBuilder();
- for(int i=0; i<=array.length-1; i++){
- if(getNumberOfOccurrences(array[i]) != exactNumberOfDuplications){
- setValuesToKeep(String.valueOf(array[i]));
- }
- }
- return convertStringArrToIntegerArr();
- }
-
- public int getNumberOfOccurrences(int value){
- int numberOfOccurrences = 0;
- for (int element : array){
- if(element ==value){
- numberOfOccurrences++;
- }
- }
- return numberOfOccurrences;
- }
-
- public void resetStringBuilder(){
- valuesToKeep.delete(0,valuesToKeep.length());
- }
-
- public void setValuesToKeep(String value){
- valuesToKeep.append(value + ",");
- }
-
- public String[] getValuesToKeep(){
- String tempString = valuesToKeep.toString();
- String[] valuesToKeepArray = {};
- if(tempString.length()<1){
- return valuesToKeepArray;
- }
- valuesToKeepArray = tempString.split(",");
- return valuesToKeepArray;
- }
-
- public Integer[] convertStringArrToIntegerArr(){
- String[] stringValuesToKeepArray = getValuesToKeep();
- Integer[] valuesToKeepArray = new Integer [stringValuesToKeepArray.length];
- for (int i=0; i<stringValuesToKeepArray.length; i++){
- valuesToKeepArray[i] = Integer.parseInt(stringValuesToKeepArray[i]);
- }
- return valuesToKeepArray;
- }
- }
|