我有一个包含重复元素的 Ruby 数组。
array = [1,2,2,1,4,4,5,6,7,8,5,6]
如何在不使用循环和迭代的情况下,从此数组中删除所有重复元素并保留所有唯一元素?
我有一个包含重复元素的 Ruby 数组。
array = [1,2,2,1,4,4,5,6,7,8,5,6]
如何在不使用循环和迭代的情况下,从此数组中删除所有重复元素并保留所有唯一元素?
您可以返回交集。
a = [1,1,2,3]
a & a
这也将删除重复项。
a | a
(求并集)也可以达到同样的效果。 - Cezararray.uniq # => [1, 2, 4, 5, 6, 7, 8]
还有一点可能会对你有用的是,uniq
可以接受一个块参数。所以如果你有一个键的数组:
["bucket1:file1", "bucket2:file1", "bucket3:file2", "bucket4:file2"]
如果你想知道哪些文件是唯一的,可以使用如下方法:
a.uniq { |f| f[/\d+$/] }.map { |p| p.split(':').last }
uniq
发送到该数组,则会返回与使用块相同的值。 - hdgarrood如果有人想要找到一种方法来删除所有重复值的实例,请参见"如何在Ruby数组中高效提取重复元素?"。
a = [1, 2, 2, 3]
counts = Hash.new(0)
a.each { |v| counts[v] += 1 }
p counts.select { |v, count| count == 1 }.keys # [1, 3]
`a = [1, 2, 2, 3]
a.find_all { |x| a.count(x) == 1 } # [1, 3]`
(翻译说明:这是一段 Ruby 代码,用于查找数组中只出现过一次的元素并返回一个由这些元素组成的新数组。将代码中的变量名或关键字直接翻译成中文即可,不需要额外解释。) - Tim Wright如果有人在意,这只是另一种选择。
您还可以使用数组的to_set
方法,将数组转换为集合。按照定义,集合元素是唯一的。
[1,2,3,4,5,5,5,6].to_set => [1,2,3,4,5,6]
to_set
将会分配4个对象,而uniq
则只会分配一个。 - Jan Klimo对我来说最简单的方法如下:
array = [1, 2, 2, 3]
Array#to_set
Array#to_set
array.to_set.to_a
# [1, 2, 3]
Array#uniq
array.uniq
# [1, 2, 3]
提供一些见解:
require 'fruity'
require 'set'
array = [1,2,2,1,4,4,5,6,7,8,5,6] * 1_000
def mithun_sasidharan(ary)
ary.uniq
end
def jaredsmith(ary)
ary & ary
end
def lri(ary)
counts = Hash.new(0)
ary.each { |v| counts[v] += 1 }
counts.select { |v, count| count == 1 }.keys
end
def finks(ary)
ary.to_set
end
def santosh_mohanty(ary)
result = ary.reject.with_index do |ele,index|
res = (ary[index+1] ^ ele)
res == 0
end
end
SHORT_ARRAY = [1,1,2,2,3,1]
mithun_sasidharan(SHORT_ARRAY) # => [1, 2, 3]
jaredsmith(SHORT_ARRAY) # => [1, 2, 3]
lri(SHORT_ARRAY) # => [3]
finks(SHORT_ARRAY) # => #<Set: {1, 2, 3}>
santosh_mohanty(SHORT_ARRAY) # => [1, 2, 3, 1]
puts 'Ruby v%s' % RUBY_VERSION
compare do
_mithun_sasidharan { mithun_sasidharan(array) }
_jaredsmith { jaredsmith(array) }
_lri { lri(array) }
_finks { finks(array) }
_santosh_mohanty { santosh_mohanty(array) }
end
运行该程序时,会产生以下结果:
# >> Ruby v2.7.1
# >> Running each test 16 times. Test will take about 2 seconds.
# >> _mithun_sasidharan is faster than _jaredsmith by 2x ± 0.1
# >> _jaredsmith is faster than _santosh_mohanty by 4x ± 0.1 (results differ: [1, 2, 4, 5, 6, 7, 8] vs [1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, ...
# >> _santosh_mohanty is similar to _lri (results differ: [1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, ...
# >> _lri is similar to _finks (results differ: [] vs #<Set: {1, 2, 4, 5, 6, 7, 8}>)
注意:这些返回的结果不正确:
lri(SHORT_ARRAY) # => [3]
finks(SHORT_ARRAY) # => #<Set: {1, 2, 3}>
santosh_mohanty(SHORT_ARRAY) # => [1, 2, 3, 1]
尝试使用异或运算符,而不使用内置函数:
a = [3,2,3,2,3,5,6,7].sort!
result = a.reject.with_index do |ele,index|
res = (a[index+1] ^ ele)
res == 0
end
print result
使用内置函数:
a = [3,2,3,2,3,5,6,7]
a.uniq
.sort!
不也是一个内置函数吗? - Carolus
[{how: "are"}, {u: "doing"}, {how: "are"}].uniq => [{:how=>"are"}, {:u=>"doing"}]
- Blaskoviczuniq!
方法返回nil
,但通常你不需要关心.uniq!
的返回值,它会在对象本身上执行操作。 - carpinchosaurio