OneBite.Dev - Coding blog in a bite size

remove duplicates from an array in Ruby

Code snippet on how to remove duplicates from an array in Ruby

  arr = [1,2,3,4,1,2]
  arr.uniq

This code snippet removes duplicates from an array in Ruby. The first line of code sets the array “arr” to contain the numbers 1, 2, 3, 4, 1, and 2. The second line of code calls the “uniq” method on the array, which removes any duplicate values and returns a new array without the duplicates. The new array is printed out, but it is up to you to save the result if you need it.

ruby