IntegerDuplicateDeleter.java 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
  2. public IntegerDuplicateDeleter(Integer[] array){
  3. super(array);
  4. }
  5. public int numberOfOccurences(Integer occurence){
  6. int counter = 0;
  7. for(int i = 0; i < array.length; i++){
  8. if(array[i].equals(occurence)){
  9. counter++;
  10. }
  11. }
  12. return counter;
  13. }
  14. public Integer[] removeDuplicates(int maxNumberOfDuplications){
  15. Integer[] occurenceArray = new Integer[100];
  16. int nums = 0;
  17. for(int i = 0; i < array.length; i++)
  18. {
  19. int occurences = numberOfOccurences(array[i]);
  20. if(occurences < maxNumberOfDuplications)
  21. {
  22. occurenceArray[nums] = array[i];
  23. nums++;
  24. }
  25. }
  26. Integer[] result = new Integer[nums];
  27. for(int i = 0; i < nums; i++)
  28. {
  29. result[i] = occurenceArray[i];
  30. }
  31. return result;
  32. }
  33. public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications){
  34. Integer[] newArray=new Integer[100];
  35. int nums = 0;
  36. for(int i = 0; i < array.length; i++)
  37. {
  38. int occurences = numberOfOccurences(array[i]);
  39. if(occurences != exactNumberOfDuplications)
  40. {
  41. newArray[nums]=array[i];
  42. nums++;
  43. }
  44. }
  45. Integer[] result = new Integer[nums];
  46. for(int i = 0; i < nums; i++)
  47. {
  48. result[i]=newArray[i];
  49. }
  50. return result;
  51. }
  52. }