J
J
JSxNeet2017-10-20 02:19:01
ruby
JSxNeet, 2017-10-20 02:19:01

How to get all indexes of a given element in an array?

Is it possible to find out all indexes of a given element in an array? If so, how? I tried to solve this problem through the "index" method, but it returns the index only for the first element in the array. Thanks in advance for your reply.
Array example:
[ "|" , "a", "b", "|", "c," "d" , "f" , "|" , "|" ]
Example task
Get all indexes with value "|" .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2017-10-20
@JSxNeet

l=["|", "a", "b", "|", "c," "d", "f", "|", "|"]
p l.each_index.select {|i| l[i]=="|"}

> [0, 3, 6, 7]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question