-3

The problem with this code is that it will print out 5 9 9, instead of just 5,9. Because there is a third 9 in the array. What am I missing?

Edit: I need to write a function that will get the duplicates from the array given. I am trying to do this, but it is printing out a 5,9,9 instead of 5,9.

Edit 2: Well I figured it out after reading up on HashSet and got it to work using the code below. I hope this can help others with the same problem.

import java.util.HashSet;


public class Duplicator {

/**
 * @param args
 */
public static void main(String[] args) {

    int[] a = {3,5,5,8,9,9,9};

    HashSet<Integer> hash = new HashSet<Integer>();

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

        for(int j = i+1; j< a.length; j++){

            if(a[i] == a[j]){

                hash.add(a[i]);

            }
        }


    }

    System.out.println(hash);

    }
}
Adam
  • 3
  • 1
  • 1
  • 4
  • What is the question here Adam? – kuriouscoder Mar 06 '13 at 02:41
  • Many of the duplicates of this over on the right side of the screen should be able to help. – Brian Roach Mar 06 '13 at 02:44
  • There are better methods to go about this. This code looks like it'll take O(n^2) worse case. That's no good. You can do this in O(n) time if you just keep rolling set and check against it as you progress through the array. Also if it's always going to be sorted, you can do this without extra data structures and still preserve O(n), just keep track of the previous index. – Franklin Mar 06 '13 at 03:10
  • http://javaconceptoftheday.com/java-program-to-find-duplicate-elements-in-an-array/ – Mukesh Kumar Swami Jan 10 '17 at 19:12

4 Answers4

2

You are trying to find the number who are duplicates, but you are actually just comparing with the previous element.So it prints the number every times it is the same as the one preceding it, which means: 5, 9 and 9.

You can add a variable to keep track of the last element printed. The code would still break if the array is not sorted, though.

You can also use Sets:

public static void main(String[] args) {
  int[] a = {3,5,5,8,9,9,9};

  Set<Integer> encounteredNumbers = new HashSet<Integer>();
  Set<Integer> duplicateNumbers = new LinkedHashSet<Integer>(); // LinkedHashSet to keep in same order as encountered.
  for (int i : a) {
    if (encounteredNumbers.contains(i)) {
      duplicateNumbers.add(i);
    }
    encounteredNumbers.add(i);
  }

  System.out.println(duplicateNumbers);
}
Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
0

Try advancing through the array while keeping track of what value the last element was.

If the value changed, set boolean duplicateFound to false.

If the value did not change, and duplicateFound is false, set duplicateFound to true and print.

Also be sure to sort the array first if it may be unsorted.

Patashu
  • 21,443
  • 3
  • 45
  • 53
0

You can use a set to avoid duplicate values. For example :

Set<Integer> values=new HashSet<Integer>();
values.add(5);
values.add(9);
values.add(9);
for(Integer val: values) {
System.out.println(val);
}

it prints :

5
9

Another example :

int[] a = {3,5,5,8,9,9,9};
List<Integer> list=new ArrayList<Integer>();
for(int i=0; i<a.length; i++) {
if(!list.contains(a[i])) {
list.add(a[i]);
}

}

for(int index=0; index<list.size(); index++) {
System.out.println(list.get(index));// prints the value in the corresponding index.
}

It prints as follows:

3
5
8
9
Visruth
  • 3,430
  • 35
  • 48
-1

I am not clear about the question. If you need to eliminate duplicates a simple way depending on the size of the input array would be to construct a java.util.Set and iterate through it.

kuriouscoder
  • 5,394
  • 7
  • 26
  • 40