arr是字符串数组,例如:["hello", "world", "stack", "overflow", "hello", "again"].
什么是简单而优雅的方法来检查是否arr有重复,如果是,则返回其中一个(无论哪个).
例子:
["A", "B", "C", "B", "A"] # => "A" or "B"
["A", "B", "C"] # => nil
Run Code Online (Sandbox Code Playgroud) 我有一个排序数组:
[
'FATAL <error title="Request timed out.">',
'FATAL <error title="Request timed out.">',
'FATAL <error title="There is insufficient system memory to run this query.">'
]
Run Code Online (Sandbox Code Playgroud)
我想得到这样的东西,但它不一定是哈希:
[
{:error => 'FATAL <error title="Request timed out.">', :count => 2},
{:error => 'FATAL <error title="There is insufficient system memory to run this query.">', :count => 1}
]
Run Code Online (Sandbox Code Playgroud) 我有一个包含一些元素的数组.如何才能获得数组中每个元素的出现?
例:
鉴于:
a = ['cat', 'dog', 'fish', 'fish']
Run Code Online (Sandbox Code Playgroud)
结果:
a2 #=> {'cat' => 1, 'dog' => 1, 'fish' => 2}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?