Getting the indexes of duplicate elements in arrays (Ruby) -
i have array in ruby has duplicate elements. e.g.:
fruits = ["apples", "bananas", "apples", "grapes", "apples"] when following:
fruits.index("apples") # returns 0 i first occurrence of "apples" is, in case, fruits[0]. there way can run similar above code , indexes of other occurrences of "apples"? if can't run similar above code, how else can indexes of duplicate elements?
taking page procedural languages, write:
fruits.each_index.select { |i| fruits[i]=="apples" } #=> [0, 2, 4]
Comments
Post a Comment