我有一个包含重复元素的Ruby数组。
array = [1,2,2,1,4,4,5,6,7,8,5,6]
如何在不使用for循环和迭代的情况下从此数组中删除所有重复元素,同时保留所有唯一元素?
我有一个包含重复元素的Ruby数组。
array = [1,2,2,1,4,4,5,6,7,8,5,6]
如何在不使用for循环和迭代的情况下从此数组中删除所有重复元素,同时保留所有唯一元素?
Answers:
[{how: "are"}, {u:"doing"}, {how: "are"}].uniq => [{:how=>"are"}, {:u=>"doing"}]
.uniq!
结果是否对对象本身起作用
您可以返回交点。
a = [1,1,2,3]
a & a
这也会删除重复项。
a | a
(联合)将完成相同的技巧。
您可以使用uniq方法删除重复的元素:
array.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
不带块的情况下发送到该数组将返回与您的块相同的值。
如果有人在寻找删除重复值的所有实例的方法,请参阅“ 如何有效地提取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]
只是提供一些见解:
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]