0
aa = [ %w[Someone 1],
      %w[Bla 2]]

p aa.assoc("Someone")
p aa.assoc("Bla")

# Result:
# ["Someone", "1"]
# ["Bla", "2"]

p aa.rassoc("1")
p aa.rassoc("2")

# Result:
# ["Someone", "1"]
# ["Bla", "2"]

is ruby assoc array a hashtable internally? what is lookup time complexity? for example is this call linear or O(1) aa.assoc("Someone")

thanks

BufBills
  • 8,005
  • 12
  • 48
  • 90
  • O(n) in [CRuby](http://ruby-doc.org/core-2.2.0/Array.html#method-i-assoc). Btw. I'd discourage rely on current/specific implementation detail, unless it's explicitly mentioned in official docs. – joanbm Nov 08 '15 at 03:54

1 Answers1

1

This discussion implies it is a linear search, not a hash table: http://error.news/question/4883140/does-ruby-arrayassoc-use-linear-search/

This makes sense because the data type you initialize is just an array. assoc() is just a function operating over it.

There is a Hash data type though: http://ruby-doc.org/core-1.9.3/Hash.html

modal_dialog
  • 733
  • 5
  • 12