我有一个值“狗”和一个数组[“猫”、“狗”、“鸟”]。

如何在不循环的情况下检查数组中是否存在它?是否有一种简单的方法来检查该值是否存在?


当前回答

这不仅会告诉你它存在,还会告诉你它出现的次数:

 a = ['Cat', 'Dog', 'Bird']
 a.count("Dog")
 #=> 1

其他回答

如果需要多次检查任何键,请将arr转换为哈希,然后检查O(1)

arr = ['Cat', 'Dog', 'Bird']
hash = arr.map {|x| [x,true]}.to_h
 => {"Cat"=>true, "Dog"=>true, "Bird"=>true}
hash["Dog"]
 => true
hash["Insect"]
 => false

Hash#has_key的性能?与Array#include相比?

Parameter              Hash#has_key?                 Array#include 

Time Complexity         O(1) operation                O(n) operation 

Access Type             Accesses Hash[key] if it      Iterates through each element
                        returns any value then        of the array till it
                        true is returned to the       finds the value in Array
                        Hash#has_key? call
                        call    

对于一次性检查,使用include?很好

如果要在MiniTest单元测试中执行此操作,可以使用assert_includes。例子:

pets = ['Cat', 'Dog', 'Bird']
assert_includes(pets, 'Dog')      # -> passes
assert_includes(pets, 'Zebra')    # -> fails 

有趣的事实,

可以使用*检查case表达式中的数组成员资格。

case element
when *array 
  ...
else
  ...
end

注意when子句中的小*,它检查数组中的成员身份。

splat运算符的所有常见魔术行为都适用,所以例如,如果数组实际上不是一个数组,而是一个元素,那么它将匹配该元素。

有几个答案建议阵列#包括?,但有一个重要的警告:查看源代码,甚至是Array#include?确实执行循环:

rb_ary_includes(VALUE ary, VALUE item)
{
    long i;

    for (i=0; i<RARRAY_LEN(ary); i++) {
        if (rb_equal(RARRAY_AREF(ary, i), item)) {
            return Qtrue;
        }
    }
    return Qfalse;
}

在不循环的情况下测试单词存在的方法是为数组构造一个trie。有很多trie实现(谷歌“ruby trie”)。我将在本例中使用随机trie:

a = %w/cat dog bird/

require 'rambling-trie' # if necessary, gem install rambling-trie
trie = Rambling::Trie.create { |trie| a.each do |e| trie << e end }

现在,我们已经准备好测试数组中各种单词的存在,而无需在O(log n)时间内对其进行循环,语法简单性与array#include?,使用子线Trie#include?:

trie.include? 'bird' #=> true
trie.include? 'duck' #=> false

如果要返回值而不仅仅是true或false,请使用

array.find{|x| x == 'Dog'}

如果列表中存在“Dog”,则返回该值,否则为零。