2

Hey I've been messing around in java and created this program that will go through an array and find all duplicates in the array and its working which is great! but there is one small problem if I run the program and there is more than 2 duplicates of one value it will output two duplicates found for the same value - this will probably explain it better

Output

Duplicates in Array: 1
Duplicates in Array: 8
Duplicates in Array: 8

I've been stuck on this for some time now I know its probably some small error but if anyone could help me out that would be awesome, the code is below, thanks

import java.util.Arrays;
public class Duplicates {
    public static void main (String[] args) {
        int[] values = { 8, 5, 9, 8, 6, 13, 33, 1, 98, 12, 8, 1 };
        Arrays.sort(values);
        for(int i = 1; i < values.length; i++) {
            if(values[i] == values[i - 1]) {
                System.out.println("Duplicates in Array: " + values[i]);
            }
        }
    }
}
afzalex
  • 8,598
  • 2
  • 34
  • 61
Aaron
  • 23
  • 5
  • 3
    You should use a HashSet, which allows only unique elements, and will automatically disregard duplicate values. – Afforess Oct 26 '14 at 21:54
  • http://stackoverflow.com/questions/7414667/identify-duplicates-in-a-list seems to be a very similar question – eee Oct 26 '14 at 21:59
  • Well, if you need to find how many duplicates each value has, then you can easily create a Map, then iterate over the array and put your array value as a map key, and increment map value (for key) whenever such entry already exists. – Rafal G. Oct 26 '14 at 22:00
  • I think OPs intention was to not use Collections, but only Arrays... – Mr. Polywhirl Oct 26 '14 at 22:12

2 Answers2

2

If I understand your question, then you could add a while loop to progress past the duplicates after you report one. Like,

public static void main(String[] args) {
    int[] values = { 8, 5, 9, 8, 6, 13, 33, 1, 98, 12, 8, 1 };
    Arrays.sort(values);
    for (int i = 1; i < values.length; i++) {
        if (values[i] == values[i - 1]) {
            System.out.println("Duplicates in Array: " + values[i]);
            while (values[i] == values[i - 1]) { // <-- add this
                i++;
            }
        }
    }
}

Output is

Duplicates in Array: 1
Duplicates in Array: 8
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Ah i see now, i was actually trying to work it out using a while loop i was just making it overly complicated :p thanks! – Aaron Oct 26 '14 at 22:24
0

I'm not sure if you are allowed to use arraylists or not in your example but if you are then you can easily store the values you already have and on each for loop you can check if there are any duplicates.

for (int i = 0; i < values.length; i++) {

        for (int j = i + 1; j < values.length; j++) {
            if (values[i] == values[j] && !foundValues.contains(values[j])) {
                System.out.println("Duplicates in Array: " + values[i]);
                foundValues.add(values[i]);
            }
        }
    }

output is

Duplicates in Array: 1
Duplicates in Array: 8
Alex
  • 627
  • 3
  • 12
  • 32
  • Thanks for helping me with that :) I havent worked with arraylists before, i'm going to go on and start looking into them now thanks! – Aaron Oct 26 '14 at 22:24