PUZZLE TIME how would you implement in ruby a method that takes ["a", "b", "a", "c", "c"] and returns {"a" => 2, "b" => 1, "c" => 2} ?
ie the counts of the occurrences. A bunch of us can't find any pretty answers.
-
-
@Carols10cents Something like: a = ["a", "b", "a", "c", "c"] a.uniq.map { |x| {x => a.count(x) } } -
@BillLaboon that's pretty pretty! but it does return an array of hashes instead of one hash... would need like [] instead and then .to_h - 1 more reply
New conversation -
-
-
@Carols10cents Not sure if that's "pretty", but it's my first "straightforward" thought.Thanks. Twitter will use this to make your timeline better. UndoUndo
-
-
-
@Carols10cents arr.inject(http://Hash.new (0)) { |counts, str| counts[str] += 1; counts }Thanks. Twitter will use this to make your timeline better. UndoUndo
-
-
-
@Carols10cents I think -
@Carols10cents yeah just checked, it works - 1 more reply
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.