Finding Duplicates in Array and printing them only Once

前端 未结 6 1184

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:49

    I would use a HashMap to store the value I encounter in the array, with the count as a value. So if you encounter a 4, you would look it up in the HashMap, if it doesn't exist, you would add it with a value of 1, otherwise increment the value returned.

    You can the loop over the HashMap and get all the values and print the number of duplicates encountered in the array.

提交回复
热议问题