0

So, i am writing a program that analyzes an entire array and displays the repeated values as well as the unique values:

int dupe = 0;
int[] range = {1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 6};
for (int i = 0; i < range.length; i++) {
        for (int j = i + 1; j < range.length; j++) {
            if (range[i] == range[j]) {
                dup = range[j];
                System.out.println(dup);
            }
        }
}

The above code outputs the repeated values correctly but when the value repeats three or more times, it outputs that value many times instead of just once

1
2
2
2
3
3
3
6

How can i fix this?

For the unique value part of the program, i don't know where to start.

Thanks!

EDIT: The only Arrays class methods i can use are: binarySearch, copyOf, equals, fill, sort, and toString

I need to write my own implementation - not to use Set, HashSet etc. Or any other tools such as iterators

Zero
  • 39
  • 1
  • 6
  • Keep duplicated values in an array and then `System.out` the values, checking if there is already one. Map can help you easily on that. – Michael Laffargue Nov 17 '14 at 09:03
  • is your array is always sorted? – user902383 Nov 17 '14 at 09:07
  • for the first two comments, i cannot use those. And for the 3rd comment, yes the array will always be sorted – Zero Nov 17 '14 at 09:08
  • The first two comments do not suggest that you use anything, and the third comment is yours to begin with!!! So I strongly recommend that you write down the exact list of restrictions in your question. For example, I gave a simple answer using a `HashMap`, which has nothing to do with "`Arrays` class methods". Are there any other restrictions besides those methods? – barak manos Nov 17 '14 at 09:32
  • @barakmanos when i first submitted the comment there was an extra comment, the user must have deleted it. As for the restrictions i just need to use a simple Array, no set, hashsets,etc. – Zero Nov 17 '14 at 09:42

6 Answers6

2

You can do somthing like bellow:

int[] range = {1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 6};
boolean duplicate = false;
for (int i = 0; i < range.length; i++) {
     duplicate = false;
     for (int j = i + 1; j < range.length; j++) {
         if (range[i] == range[j]) {
             duplicate = true
         }
     }
     if(!duplicate){
         System.out.println(range[i]);
     }
}
HaMi
  • 539
  • 6
  • 23
  • I tried this method but it just outputs every value in the array – Zero Nov 17 '14 at 09:18
  • This approach is bad, use map to store walues and their occurences, complexity of your algoritm is pessimistic. You iterate n^2 times over your table. – Beri Nov 17 '14 at 09:19
  • Why are you traversing beyond when you found duplicate and why not break instead? – SMA Nov 17 '14 at 09:21
  • @Zero It is your task to print every value in the array once. Either the value is unique or it occurs multiple times. There are no other cases. So your task can be stripped down to just skip the duplicates. – Fabian Barney Nov 17 '14 at 09:35
  • @Zero, Beri, almas: It works fine. The complexity in this code is not my point and I just want to correct your solutions. It is obvious that the complexity is high and of course this is not best solution to do that. – HaMi Nov 17 '14 at 13:54
1

You can add your values to Set. it will do all work for you.

int end = arr.length;
Set<Integer> set = new HashSet<Integer>();

for(int i = 0; i < end; i++){
    set.add(arr[i]);
}

If you cannot use other data structures, here is a good solution: https://stackoverflow.com/a/17974322/2290763

Community
  • 1
  • 1
Forin
  • 1,549
  • 2
  • 20
  • 44
1

Other solution are correct but they will give O(n^2), you don't need to use two for loops. This will give you O(n)

    int[] range = {1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 6};
    int temp = range[0];
    if(temp != range[range.length-1]){
     for (int i = 1; i < range.length; i++) {
            if(temp != range[i]){
               System.out.println(temp);
               temp = range[i];
           }
     }
    }
    else
       System.out.println(temp);
Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
1

Each value in the array is either unique or occurs multiple times. There are no other cases. So your task can be stripped down to just remove the duplicates and print everything else. If the array is sorted then it is sufficient to just check against the last value while iterating to recognize duplicates.

I would do this:

int[] range = {1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 6};
Arrays.sort(range);

for (int i = 0; i < range.length; i++) {
    if (i == 0) {
        System.out.println(range[0]);
    } else if (range[i - 1] != range[i]) {
        System.out.println(range[i]);
    }
}
Fabian Barney
  • 14,219
  • 5
  • 40
  • 60
1

Try after sorting the array:

int[] range = { 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 6 };
    Arrays.sort(range);
    System.out.println(range.length > 0 ? range[0]
            : "No sufficient elements");
    for (int i = 1; i < range.length; i++) {
        if (range[i - 1] != range[i]) {
            System.out.println(range[i]);
        }
    }
SMA
  • 36,381
  • 8
  • 49
  • 73
  • @FabianBarney will try this method but i do not understand what the first print statement is doing – Zero Nov 17 '14 at 09:39
  • It prints the first number in the array or handles the special case when using an empty array - meaning an array with length equal to 0. – Fabian Barney Nov 17 '14 at 09:41
0

Edited. Try this:

    int[] range = { 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 6};
    for (int i = 0; i < range.length; i++) {
        int j = i + 1;
        for (; j < range.length && range[i] == range[j]; j++) {
            // do nothing
        }

        if (j >= i + 2) {
            System.out.println(range[i]);
            i = j;
        }

    }
}

The problem in your solution is int this part for (int i = 0; i < range.length; i++) { for (int j = i + 1; j < range.length; j++) {

For every element in array you are checking is there element with same value, and index is greater than index of element. For example, for element with index 2 (value 2) it's checking all (indexes: 3, 4, 5, 6, ...). And there are two matches (elements with index 3 and 4).

Edit: (After @FabianBarney comment)

Solution:

    String uni = "Uniques: ";
    String dup = "Duplicates: ";

    int[] range = { 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 6};

    for (int i = 0; i < range.length; i++) {
        if (i + 1 < range.length && range[i + 1] == range[i]) {
            dup += range[i] + " ";

            int j = i + 1;
            while (j < range.length && range[i] == range[j]) {
                j++;
            }
            i = j - 1;
        } else {
            uni += range[i] + " ";
        }
    }

    System.out.println(uni);
    System.out.println(dup);
djm.im
  • 3,295
  • 4
  • 30
  • 45
  • This method prints out every value at least once, so it didnt work for me – Zero Nov 17 '14 at 09:23
  • This worked for the duplicates flawlessly, now is there anything we can add to print the unique values? – Zero Nov 17 '14 at 09:55
  • When running this, it skips some repeated values sometimes – Zero Nov 17 '14 at 10:04
  • @Zero Did you try to solve for unique values? If this solution doesn't print duplicates try to debug. – djm.im Nov 17 '14 at 10:10
  • Okay i tried a few things and its working properly now thanks! – Zero Nov 17 '14 at 10:19
  • This code does not work. Counter-example: `range := {1}`. It does not print the unique value `1`. Downvoted, because it's criminal that a wrong answer was accepted. – Fabian Barney Nov 17 '14 at 10:28
  • This code still fails for unsorted arrays. Counter-example: `range := {1,2,1}`. It prints `1` twice as "uniques". – Fabian Barney Nov 17 '14 at 11:19
  • @FabianBarney Thank you for testing my code. You are right: My solution is not corect. But I did not try to cover all posible array commbinations. Why? Obviously this is homeworke. Because that Zero should solve this, and I tried help him not to solve his homework. – djm.im Nov 17 '14 at 11:37