I have an array and a hash
L = [] H = {3=>"a", 2=>"b", 1=>"c"}
And so I will iterate over the keys to get the number of times n the element occurs and append that element to an array n times
Result
L = ['a', 'a', 'a', 'b', 'b', 'c']
What's a nice way to write this with inject (or other methods that I often see in ruby code)?
array = hash.flat_map { |k,v| [v]*k }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With