Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to display only duplicate values

I'm trying to display only the duplicate value in an array which just holds names.

So for instance, my code is:

  <%= s= arrayOfStuff %> 
  <%= t= arrayOfStuff.uniq %>

which displays

["UK01USV005", "NJ08APP516", "NJ08MHF001", "UK01USV505", "NY01MHF0006", "UK01USV525", "UK01USV005", "NJ08APP515", "NJ08MHF002"]
["UK01USV005", "NJ08APP516", "NJ08MHF001", "UK01USV505", "NY01MHF0006", "UK01USV525", "NJ08APP515", "NJ08MHF002"]

so theortically when I do s-t it should give me the duplicate value which in this case is UK01USV005, however the results I get is an empty array which obviously looks like this: [].

Any ideas why that could be?

like image 466
omarArroum Avatar asked Dec 07 '22 19:12

omarArroum


1 Answers

arrayOfStuff.group_by {|e| e}.select { |k,v| v.size > 1}.keys

should work fine.

like image 195
lucapette Avatar answered Dec 20 '22 05:12

lucapette