How to count identical string elements in a Ruby array

前端 未结 14 2196
感情败类
感情败类 2020-12-02 06:34

I have the following Array = [\"Jason\", \"Jason\", \"Teresa\", \"Judah\", \"Michelle\", \"Judah\", \"Judah\", \"Allison\"]

How do I produce a count for

14条回答
  •  佛祖请我去吃肉
    2020-12-02 07:07

    This works.

    arr = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
    result = {}
    arr.uniq.each{|element| result[element] = arr.count(element)}
    

提交回复
热议问题