Getting the indexes of duplicate elements in arrays (Ruby)

来源:互联网 发布:数据挖掘导论百度云 编辑:程序博客网 时间:2024/06/10 22:44

I have an array in Ruby that has some duplicate elements. E.g.:

fruits = ["apples", "bananas", "apples", "grapes", "apples"]
-------------------------------------------------------------------
fruits.to_enum.with_index.select{|e, _| e == "apples"}.map(&:last)
fruits.each_index.select { |i| fruits[i]=="apples" }
fruits.each_with_index.select{|v, i| v == "apples"}.map(&:last)

0 0