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
9 ответов
+ 1
I think it becomes more understandable after doing Lesson 63.
0
Can you please try to implement your logic and show it here?
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
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.
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.
0
none of this helps