IntegerDuplicateDeleter.java 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
  2. public IntegerDuplicateDeleter(Integer[] array){
  3. super(array);
  4. }
  5. public Integer[] removeDuplicates(int maxNumberOfDuplications){
  6. Integer[] newArray =new Integer[array.length];
  7. int newArrayIndex= 0;
  8. //go through each element in the array
  9. for(Integer num : array) {
  10. int occurance = countOccurance(num);
  11. if(occurance < maxNumberOfDuplications) {
  12. newArray[newArrayIndex] = num;
  13. newArrayIndex++;
  14. }
  15. }
  16. //count occurrance
  17. Integer[] finalArray = new Integer[newArrayIndex];
  18. System.arraycopy(newArray, 0, finalArray, 0, newArrayIndex);
  19. return finalArray;
  20. }
  21. public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications){
  22. Integer[] newArray =new Integer[array.length];
  23. int newArrayIndex= 0;
  24. //go through each element in the array
  25. for(Integer num : array) {
  26. int occurance = countOccurance(num);
  27. if(occurance != exactNumberOfDuplications) {
  28. newArray[newArrayIndex] = num;
  29. newArrayIndex++;
  30. }
  31. }
  32. //count occurrance
  33. Integer[] finalArray = new Integer[newArrayIndex];
  34. System.arraycopy(newArray, 0, finalArray, 0, newArrayIndex);
  35. return finalArray;
  36. }
  37. private int countOccurance(Integer number){
  38. int count =0;
  39. for(Integer i : array) {
  40. if(number == i) {
  41. count++;
  42. }
  43. //
  44. //when number ==}
  45. }
  46. return count;
  47. }
  48. }