Ruby - collect same numbers from array into array of arrays

前端 未结 4 1962
难免孤独
难免孤独 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 06:46

    In case if you want to do it without order:

    ar.group_by(&:itself).values
     => [[5, 5], [2, 2, 2], [6, 6]]
    

提交回复
热议问题