Finding indices of element in array ruby -
how can find indices of elements in array has particular value in ruby?
i.e. if have array [2,3,52,2,4,1,2], there easier way use loops, indices of 2 in array? answer [0,3,6], if looking 2.
answer in get index of array element faster o(n) gives solution if want find 1 instance of given element.
a # => [2, 3, 52, 2, 4, 1, 2] b = [] # => [] a.each_with_index{|i, ind| b << ind if == 2} # => [2, 3, 52, 2, 4, 1, 2]
Comments
Post a Comment