我有一个包含重复元素的Ruby数组。
array = [1,2,2,1,4,4,5,6,7,8,5,6]
如何在不使用for循环和迭代的情况下保留所有唯一元素的同时从此数组中删除所有重复元素?
答案 0 :(得分:657)
答案 1 :(得分:79)
您也可以返回交叉路口。
a = [1,1,2,3]
a & a
这也会删除重复项。
答案 2 :(得分:41)
您可以使用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 }
答案 3 :(得分:16)
如果有人关心的话,只是另一种选择。
您还可以使用数组的to_set
方法将Array转换为Set,根据定义,set元素是唯一的。
[1,2,3,4,5,5,5,6].to_set => [1,2,3,4,5,6]
答案 4 :(得分:15)
如果有人正在寻找删除重复值的所有实例的方法,请参阅this question。
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]
答案 5 :(得分:4)
对我来说最简单的方法是这些:
array = [1, 2, 2, 3]
Array#to_set
array.to_set.to_a
# [1, 2, 3]
Array#uniq
array.uniq
# [1, 2, 3]
答案 6 :(得分:2)
仅提供一些见解:
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]
答案 7 :(得分:-1)
尝试使用Ruby中的XOR运算符:
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