Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class IntArrayProcessor {
- private int[] a;
- public IntArrayProcessor(int[] a) {
- this.a = a;
- }
- /**
- *
- * @return Array with no repeated integers.
- */
- public int[] getSet() {
- /* creates an array with the same entries and length as this.a */
- int[] duplicateA = new int[this.a.length];
- /* stores the number of repeated entries in array this.a */
- int numberOfDuplicates = 0;
- /* is the integer a duplicate or not? */
- boolean isDuplicate;
- /**
- * Counts the number of duplicates in array this.a
- */
- for (int i = 0; i < this.a.length; i++) {
- duplicateA[i] = this.a[i];
- }
- for (int i = 0; i < duplicateA.length; i++) {
- isDuplicate = false;
- for (int j = i + 1; j < this.a.length; j++) {
- if (duplicateA[i] == this.a[j]) {
- isDuplicate = true;
- }
- }
- if (isDuplicate) {
- numberOfDuplicates++;
- }
- }
- /*
- * the noDuplicate array has the lenght of the this.a array minus the
- * number of repeated entries
- */
- int[] noDuplicate = new int[this.a.length - numberOfDuplicates];
- /* to keep track of the noDuplicate indexes */
- numberOfDuplicates = 0;
- /**
- * An array with no repeated numbers
- */
- for (int i = 0; i < duplicateA.length; i++) {
- isDuplicate = false;
- for (int j = i + 1; j < this.a.length; j++) {
- if (duplicateA[i] == this.a[j]) {
- isDuplicate = true;
- }
- }
- if (!(isDuplicate)) {
- noDuplicate[numberOfDuplicates] = duplicateA[i];
- numberOfDuplicates++;
- }
- }
- return noDuplicate;
- }
Add Comment
Please, Sign In to add comment