在Ruby中删除数组中的重复元素

我有一个包含重复元素的Ruby数组。

array = [1,2,2,1,4,4,5,6,7,8,5,6]

我如何从这个数组中删除所有重复的元素,同时保留所有唯一的元素,而不使用for循环和迭代?

261095 次浏览
array = array.uniq

uniq删除数组中所有重复的元素并保留数组中所有唯一的元素。

这是Ruby语言的众多优点之一。

你可以用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 }

如果有人正在寻找一种方法来删除重复值的所有实例,请参阅“如何有效地提取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,1,2,3]
a & a

这也将删除重复项。

如果有人在乎的话,这只是另一个选择。

你也可以使用数组的to_set方法将array转换为Set,根据定义,Set元素是唯一的。

[1,2,3,4,5,5,5,6].to_set => [1,2,3,4,5,6]

尝试使用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

内置功能:

a = [3,2,3,2,3,5,6,7]


a.uniq

只是为了提供一些见解:

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]

对我来说,最简单的方法是:

array = [1, 2, 2, 3]

Array#to_set

array.to_set.to_a


# [1, 2, 3]

Array#uniq

array.uniq


# [1, 2, 3]