J
Jim Burgess
Hi,
I need to count the number of times an element occurs in an array.
At the moment I'm doing it like this:
a = ["a", "b", "a", "b", "b"]
b = a.uniq
c = []
0.upto(b.length-1) do |e|
d = 0
0.upto(a.length-1) do |f|
if b[e] == a[f]
d += 1
end
end
c << d
end
print b, c
This works, but seems really clumsy.
Can anyone give me any tips on what I could improve?
Cheers.
I need to count the number of times an element occurs in an array.
At the moment I'm doing it like this:
a = ["a", "b", "a", "b", "b"]
b = a.uniq
c = []
0.upto(b.length-1) do |e|
d = 0
0.upto(a.length-1) do |f|
if b[e] == a[f]
d += 1
end
end
c << d
end
print b, c
["a", "b"] [2, 3]
This works, but seems really clumsy.
Can anyone give me any tips on what I could improve?
Cheers.