Fastest way to find a String into an array of string Fastest way to find a String into an array of string arrays arrays

Fastest way to find a String into an array of string


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']# => #<Set: {"1.1.1.1", "1.2.3.4"}> s.include? '1.1.1.1'# => true 


You could use the Array#include method to return you a true/false.

http://ruby-doc.org/core-1.9.3/Array.html#method-i-include-3F

if ips.include?(ip) #=> true  puts 'ip exists'else  puts 'ip  doesn\'t exist'end


A faster way would be:

if ips.include?(ip)  puts "ip exists"  return 1else  puts "ip doesn't exist"  return nilend