Getting the indexes of duplicate elements in arrays (Ruby)

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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章