Finding Duplicates in Array and printing them only Once

前端 未结 6 1185

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-22 00:04

    You perform the check for every single item of the array, including the first 4, the second 4 and so on. That's why it just doesn't stop and it prints the message multiple times per duplicated element.

    You're saying you cannot use a Set and that you don't want to sort your data. My suggestion is that you loop over the array and add each duplicated item to a list. Make sure you check whether the item has already been added. (or use a Set :) )

    Then loop over the list and print those items.

提交回复
热议问题