Assuming I have the following array:
views = [ { :user_id => 1, :viewed_at => '2012-06-29 17:03:28 -0400' }, { :user_id => 1, :viewed_at => '2012-06-29 17:04:28 -0400' }, { :user_id => 2, :viewed_at => '2012-06-29 17:05:28 -0400' }, { :user_id => 3, :viewed_at => '2012-06-29 17:06:28 -0400' }, { :user_id => 1, :viewed_at => '2012-06-29 17:07:28 -0400' }, { :user_id => 1, :viewed_at => '2012-06-29 17:08:28 -0400' }, { :user_id => 3, :viewed_at => '2012-06-29 17:09:28 -0400' }, { :user_id => 3, :viewed_at => '2012-06-29 17:16:28 -0400' }, { :user_id => 3, :viewed_at => '2012-06-29 17:26:28 -0400' }, { :user_id => 3, :viewed_at => '2012-06-29 17:36:28 -0400' }, { :user_id => 1, :viewed_at => '2012-06-29 17:47:28 -0400' }, { :user_id => 2, :viewed_at => '2012-06-29 17:57:28 -0400' }, { :user_id => 3, :viewed_at => '2012-06-29 17:67:28 -0400' }, { :user_id => 1, :viewed_at => '2012-06-29 17:77:28 -0400' } ]
Assuming the array is sorted by viewed_ax
If I want to get the last hash in the views array for a specific user_id , I could do the following:
views.reverse.detect { |view| view[:user_id] == 1 }
where discovery returns the first element in an enumerable, where the block evaluates to true.
My question is: I assume that there is O(n) cost of the reverse method, so how can I detect in the opposite direction without changing the array? Or is the reverse method not O(n) ?