📜  红宝石 | Enumerator::Lazy#uniq函数

📅  最后修改于: 2022-05-13 01:54:50.535000             🧑  作者: Mango

红宝石 | Enumerator::Lazy#uniq函数

Ruby 中的 uniq函数用于从给定数组中查找唯一的元素集。

示例 1:

# Initialising an array with blocks as elements
xs = [ [1, "GFG", 10], [1, "GFG", 20], 
    [2, "Geeks", 30], [2, "Geeks", 40] ].to_enum.lazy
   
# Calling uniq function for finding uniq element block
us = xs.uniq{ |x| x[0] }.map{ |x| [x[0], x[1]] }
   
# Produces result in blocks
puts us.to_a.inspect

输出:

[[1, "GFG"], [2, "Geeks"]]

示例 2:

# Initialising an array
ns = [1, 4, 6, 1, 2].to_enum.lazy
  
# calling the uniq function for finding 
# uniq values form the above array
puts ns.uniq.to_a.inspect

输出:

[1, 4, 6, 2]