Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Remove all instances of a duplicate value inside an array

I have one array and I want to remove ALL instances of a duplicate value inside of that array.

arr = [1, 1, 2, 3, 4, 4, 5]

I want the expected output:

=> [2, 3, 5]

What would be the best way to do this?

like image 982
endex Avatar asked Oct 19 '25 14:10

endex


1 Answers

p arr.group_by(&:itself).reject{|k,v|v.count>1}.keys

Output

[2, 3, 5]
like image 106
Rajagopalan Avatar answered Oct 22 '25 04:10

Rajagopalan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!