HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyrailsCritical

Remove duplicate elements from array in Ruby

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
arrayfromremoveduplicaterubyelements

Problem

I have a Ruby array which contains duplicate elements.

array = [1,2,2,1,4,4,5,6,7,8,5,6]


How can I remove all the duplicate elements from this array while retaining all unique elements without using for-loops and iteration?

Solution

array = array.uniq


uniq removes all duplicate elements and retains all unique elements in the array.

This is one of many beauties of the Ruby language.

Code Snippets

array = array.uniq

Context

Stack Overflow Q#8365721, score: 813

Revisions (0)

No revisions yet.