快速检查Ruby数组中的所有项是否都是唯一的

And*_*rew 3 ruby

我正在寻找一种快速简便的方法来检查数组中的所有项目是否都是唯一的.

unique = ['one', 'two']
unique = []
not_unique = ['one', 'one', 'two']
Run Code Online (Sandbox Code Playgroud)

Lar*_*nal 6

# As simple as possible:
not_unique == not_unique.uniq

# or perhaps
not_unique.size == not_unique.uniq.size
Run Code Online (Sandbox Code Playgroud)


Jim*_*did 6

array & array == array
Run Code Online (Sandbox Code Playgroud)

是另一种选择。