Remove from the array elements that are repeated

后端 未结 5 1179
执念已碎
执念已碎 2020-12-12 01:37

What is the best way to remove from the array elements that are repeated. For example, from the array

a = [4, 3, 3, 1, 6, 6]

need to get

5条回答
  •  攒了一身酷
    2020-12-12 02:05

    arr = [4, 3, 3, 1, 6, 6]
    
    arr.
      group_by {|e| e }.
      map {|e, es| [e, es.length] }.
      reject {|e, count| count > 1 }.
      map(&:first)
    # [4, 1]
    

提交回复
热议问题