ruby - Array filter using keep_if -


i have array this:

array = ["john-56", "admin-57", "duke-58", "duke-65",           "john-56", "admin-57", "roger-65", "roger-15"] 

i want keep elements duplicated, in case expect result:

["admin-57","admin-57","john-56","john-56"] 

i've tried using keep_if method this:

array.keep_if { |x,y| x==y } 

but leaves array empty.

maybe not more efficient, 1 line:

array.select { |x| array.count(x) == 2 }.uniq 

@edit(thanks jounty)

if have values can appear more 2 times

array.select { |x| array.count(x) > 1 }.uniq 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -