Ruby - collect same numbers from array into array of arrays

前端 未结 4 1995
难免孤独
难免孤独 2021-01-28 06:02

I have created a very ugly script to collect same numbers from an array. I don\'t think this is a very Ruby way :) Anyone could provide a more clean solution?

ar         


        
4条回答
  •  青春惊慌失措
    2021-01-28 07:01

    just another oneliner

    arr = [5, 5, 2, 2, 2, 6, 6] 
    arr.uniq.map {|e| [e]*arr.count(e) }
    # => [[5, 5], [2, 2, 2], [6, 6]] 
    

提交回复
热议问题