Guest User

Untitled

a guest
Jul 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. public class IntArrayProcessor {
  2.  
  3. private int[] a;
  4.  
  5. public IntArrayProcessor(int[] a) {
  6. this.a = a;
  7. }
  8.  
  9. /**
  10. *
  11. * @return Array with no repeated integers.
  12. */
  13. public int[] getSet() {
  14. /* creates an array with the same entries and length as this.a */
  15. int[] duplicateA = new int[this.a.length];
  16.  
  17. /* stores the number of repeated entries in array this.a */
  18. int numberOfDuplicates = 0;
  19.  
  20. /* is the integer a duplicate or not? */
  21. boolean isDuplicate;
  22.  
  23. /**
  24. * Counts the number of duplicates in array this.a
  25. */
  26. for (int i = 0; i < this.a.length; i++) {
  27. duplicateA[i] = this.a[i];
  28. }
  29.  
  30. for (int i = 0; i < duplicateA.length; i++) {
  31. isDuplicate = false;
  32. for (int j = i + 1; j < this.a.length; j++) {
  33. if (duplicateA[i] == this.a[j]) {
  34. isDuplicate = true;
  35. }
  36. }
  37. if (isDuplicate) {
  38. numberOfDuplicates++;
  39. }
  40. }
  41.  
  42. /*
  43. * the noDuplicate array has the lenght of the this.a array minus the
  44. * number of repeated entries
  45. */
  46. int[] noDuplicate = new int[this.a.length - numberOfDuplicates];
  47.  
  48. /* to keep track of the noDuplicate indexes */
  49. numberOfDuplicates = 0;
  50.  
  51. /**
  52. * An array with no repeated numbers
  53. */
  54. for (int i = 0; i < duplicateA.length; i++) {
  55. isDuplicate = false;
  56. for (int j = i + 1; j < this.a.length; j++) {
  57. if (duplicateA[i] == this.a[j]) {
  58. isDuplicate = true;
  59. }
  60. }
  61. if (!(isDuplicate)) {
  62. noDuplicate[numberOfDuplicates] = duplicateA[i];
  63. numberOfDuplicates++;
  64. }
  65. }
  66. return noDuplicate;
  67. }
Add Comment
Please, Sign In to add comment