Fastest way to find a String into an array of string

前端 未结 5 1687
被撕碎了的回忆
被撕碎了的回忆 2021-02-12 18:18

The script has to verify if one predefined IP is present in a big array of IPs. Currently I code that function like this (saying that \"ips\" is my array of IP and \"ip\" is the

5条回答
  •  醉酒成梦
    2021-02-12 18:34

    You can use Set. It is implemented on top of Hash and will be faster for big datasets - O(1).

    require 'set'
    s = Set.new ['1.1.1.1', '1.2.3.4']
    # => # 
    s.include? '1.1.1.1'
    # => true 
    

提交回复
热议问题