How can I find double entries in arrays?
The Code:
list = [1,1,2,2,3,3,4,5]
def ungerade_zahlen( odd )
number = 0
odd.each do |element|
if element % 2 == 1
number +=1
end
end
puts number
return number
end
ungerade_zahlen(list)
I don’t want to find all odd entries, only the different entries.
For example: [1,2,1,5,7,9,5,4,3] -> I want to Count 5 entries
[1,3,5,7,9]!
I would be happy if someone can help me please!
migatt
2
Mi Gatt wrote in post #1185679:
How can I find double entries in arrays?
I don’t want to find all odd entries, only the different entries.
For example: [1,2,1,5,7,9,5,4,3] -> I want to Count 5 entries
[1,3,5,7,9]!
I would be happy if someone can help me please!
Here , Hash is use as e Set :
def uz(l)
l.each_with_object( {} ) { |e,h| h[e]=1 if e%2==1}.keys.sort
end
p uz [1,2,1,5,7,9,5,4,3]
migatt
3
With the variable ‘list’ set as in your posting, the expression
list.uniq
returns an array of uniq elements in list.
migatt
4
Ronald F. wrote in post #1185682:
With the variable ‘list’ set as in your posting, the expression
list.uniq
returns an array of uniq elements in list.
true, so :
def uz(l)
l.uniq.select {|a|a%2==1}.sort
end