Get index of array element faster than O(n)

The Solution to Get index of array element faster than O(n) is


Why not use index or rindex?

array = %w( a b c d e)
# get FIRST index of element searched
puts array.index('a')
# get LAST index of element searched
puts array.rindex('a')

index: http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-index

rindex: http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-rindex

~ Answered on 2012-07-24 08:05:14


Most Viewed Questions: