-1

I am just trying a simple program on finding duplicates in given array.

/* Using HashSet */
int[] arrays = {1,2,4,5,4,2};
Set<Integer> hs = new HashSet<Integer>();
for(int ar:arrays)
{
    if(!hs.add(ar))
    {
        System.out.println("Dupicate is:" +ar);             
    }           
}       

/* Nested for loop */
int arraySearch[] = {2,2,3,4,5,6,7,7,7,8,10};
int m = 0; boolean flag = true;
for(int i=0; i<arraySearch.length; i++)
{
    flag=true;
    for(int j=i+1; j<arraySearch.length; j++)
    {
        if(arraySearch[i] == arraySearch[j])
        {
            m=arraySearch[i];
            flag = false;
            break;
        }
    }
    if(flag == false)
    {
        System.out.println(m);
    }   
}

Both the approaches are giving correct result but the problem which I am facing is if there are two numbers repeated one after another its getting printed twice. say int[] arrays = {1,1,2,4,5,4,2};

Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
  • What exactly is the problem you are having? (of course, array can contains duplicate values but that's normal) – Daniele Feb 20 '19 at 19:40
  • Possible duplicate of [Identify duplicates in a List](https://stackoverflow.com/questions/7414667/identify-duplicates-in-a-list) – Samuel Philipp Feb 20 '19 at 19:45

3 Answers3

1

The number gets printed more than once if the duplicates are more than one. To get it printed only once the code has to be changed. Try doing this

Set<Integer> hs=new HashSet<Integer>();
Set<Integer> duplicate= new HashSet<Integer>();
for(int ar:arrays)
{
    if(!hs.add(ar))
    {
        duplicate.add(ar); 
    }           
}

This way the duplicate printing can be avoided. After this the elements in the set can be displayed using a loop.

0

If you are on Java 8 or further, you can do it the following way:

int[] arrays = {1,1,2,4,5,4,2};
List<Integer> hs = IntStream.of(arrays).boxed().collect(Collectors.toList());           
hs.stream().filter(i -> Collections.frequency(hs, i) > 1)
   .collect(Collectors.toSet()).forEach(System.out::println);
VHS
  • 9,534
  • 3
  • 19
  • 43
  • Note that the call to Collections.frequency within the filter lambda. That will iterate across the entire collection to compute the frequency, for each element of the collection. This takes us back to square performance. – Thomas Bitonti Feb 20 '19 at 20:44
0

Are you seeing additional output when there are doubles, or when there are triples?

For example, for this input:

int arraySearch[] = {2,2,3,4,5,6,7,7,7,8,10};

There will be two detections of 7. That there are two detections is a result of one loop testing element 6 against elements 7 through 10, and a second loop testing element 7 against elements 8 through 10:

first detection: {2,2,3,4,5,6,(7),(7),7,8,10};
second detection: {2,2,3,4,5,6,7,(7),(7),8,10};

If the array is in ascending order, the test could be done with a single loop. With the array in random order, some tracking of which elements have been detected as having duplicates seems necessary.

Thomas Bitonti
  • 1,179
  • 7
  • 14