I am doing a program to eliminate surpluses from an array, if we have more elements than maxOcurrences
we omit them.
For example
[20,37,20,21] = > [20,37,21]
My problem in particular is to access the indexes of a hash, this is my code:
def delete_nth(elements,maxOcurrences)
if maxOcurrences < 1
return []
end
newArray = []
aparisons = {}
elements.each do |element|
occurrences = aparisons[String(element)]
puts "a" << String(occurrences)
if occurrences.nil?
newArray.push(element)
aparisons["element"]=1
puts aparisons
else
puts "e" << String(occurrences)
if occurrences < maxOcurrences
puts "i" << String(occurrences)
newArray.push(element)
aparisons["element"]=occurrences + 1
puts "j"<<aparisons
end
end
end
return newArray
end
Entry
delete_nth ([20,37,20,21], 1)
Exit
a {"element" = > 1} to {"element" = > 1} to {"element" = > 1} to {"element" = > 1}
How can I correct it? At the moment I try to access the indexes by aparisons[String(element)]