0

count the number of times each number appears in the array

i want my code to count how many times each number appears in the array. How do i do that? https://code.sololearn.com/cW0ZS0KpKb4W/?ref=app

3rd Oct 2021, 5:57 PM
Bobby Dike
Bobby Dike - avatar
6 Réponses
+ 1
I think it becomes more understandable after doing Lesson 63.
4th Oct 2021, 8:26 AM
Coding Cat
Coding Cat - avatar
0
Can you please try to implement your logic and show it here?
3rd Oct 2021, 6:44 PM
Atul [Inactive]
0
the array size is 500 and it generates random numbers between 1 to 500, so some numbers appear more than once. i want the code to show me every number and how many times they appear. for example: 1 appeared 5 times 2 appeared 1 times 3 appeared 3 times 4 appeared 7 times up until... 500 appeared 2 times
3rd Oct 2021, 6:51 PM
Bobby Dike
Bobby Dike - avatar
0
Maybe like this way: Create a map with keys from 1 to 5oo. Prefill each value with 0. So you will have a map like this: Key, Value 1 , 0 2 , 0 3 , 0 . . 500, 0 Then loop over array R, get the value from map for this R[i] as key, increase this value by one, and put it back to map map.put(R[i],(map.get(R[i])+1) After that loop over the map and get the output you wish.
3rd Oct 2021, 8:19 PM
Coding Cat
Coding Cat - avatar
0
Martin Taylor yes, I'm with you. I saw only the consecutive numbers in the question. But if one needs the zero numbers, it's also possible to consider the missing keys at output time.
3rd Oct 2021, 9:04 PM
Coding Cat
Coding Cat - avatar
0
none of this helps
4th Oct 2021, 8:00 AM
Bobby Dike
Bobby Dike - avatar