Finding Duplicates in Array and printing them only Once

前端 未结 6 1190

I am trying to loop through my array and find all the numbers that are repeating more than once:

E.G: if there is 1 1 2 3 4

It should print saying \

6条回答
  •  一个人的身影
    2021-01-21 23:40

    Just add the number you will find duplicated to some structure like HashSet or HashMap so you can find it later when you will detect another duplication.

    Set printed = new HashSet();
    
      for(int i=0; i

    Better O(n) alghorithm:

    Set printed = new HashSet();
    
      for(int i=0; i

提交回复
热议问题