Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Remove Duplicate Elements from TypeScript Array
Next article icon

How to Remove Duplicate Elements from Array in Ruby?

Last Updated : 02 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

This article focuses on discussing how to remove duplicate elements from an array in Ruby. In Ruby, an array is a fundamental data structure that stores a collection of elements in a specific order. Various methods and approaches can be used to remove duplicate elements from an array.

Removing Duplicate Elements from an Array in Ruby

Below are the possible approaches to removing duplicate elements from the array in Ruby.

Approach 1: Using the Uniq Method

  1. In this approach, we are using the uniq method in Ruby to remove duplicate elements from an array.
  2. The uniq method returns a new array with unique elements, preserving the original order.
  3. This approach provides a simple and concise way to remove duplicates from an array without modifying the original array.

In the below example, uniq method is used to remove duplicate elements from an array in ruby.

Ruby
arr = [1, 2, 2, 3, 4, 4, 5] arr_unique = arr.uniq puts "Array Elements: #{arr.inspect}" puts "Array after Removing Duplicates: #{arr_unique.inspect}" 

Output:

Screenshot-2024-03-27-at-15-44-29-428gwm7q6---Ruby---OneCompiler

Approach 2: Using Loop

  1. In this approach, we are using a loop to iterate through the array and filter out duplicate elements.
  2. The loop checks if each element is already present in the arr_unique array and only adds it if it's not already included.
  3. This method manually removes duplicates by comparing each element, resulting in an array with unique elements while preserving the original order.

In the below example, loop is used to remove duplicate elements from array in ruby.

Ruby
arr = [1, 2, 2, 3, 4, 4, 5] arr_unique = [] arr.each { |element| arr_unique << element unless arr_unique.include?(element) } puts "Array Elements: #{arr.inspect}" puts "Array after Removing Duplicates: #{arr_unique.inspect}" 

Output:

Screenshot-2024-03-27-at-15-44-29-428gwm7q6---Ruby---OneCompiler

Approach 3: Using | (pipe) Operator

  1. In this approach, we are using the | (pipe) operator in Ruby to create a new array with unique elements by combining the original array with an empty array [].
  2. The | operator performs a union operation, ensuring that duplicate elements are removed, resulting in an array with unique elements while maintaining the original order.

In the below example, | (pipe) operator is used to remove duplicate elements from array in ruby.

Ruby
arr = [1, 2, 2, 3, 4, 4, 5] arr_unique = arr | [] puts "Array Elements: #{arr.inspect}" puts "Array after Removing Duplicates: #{arr_unique.inspect}" 

Output:

Screenshot-2024-03-27-at-15-44-29-428gwm7q6---Ruby---OneCompiler


Next Article
Remove Duplicate Elements from TypeScript Array

G

gauravgandal
Improve
Article Tags :
  • Ruby

Similar Reads

  • How to Remove Duplicate Elements from NumPy Array
    In this article, we will see how to remove duplicate elements from NumPy Array. Here we will learn how to Remove Duplicate Elements from a 1-D NumPy Array and 2-D NumPy Array. Input1: [1 2 3 4 5 1 2 3 1 2 9]Output1: [1 2 3 4 5 9]Explanation: In this example, we have removed duplicate elements from o
    7 min read
  • How to Remove duplicate elements from array in JavaScript ?
    In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array. These are the following ways: Table of Content Using filter() MethodUsing set() MethodUsing forEach() MethodUsing reduce() MethodUsing index
    4 min read
  • How to Remove Duplicate Elements from an Array using Lodash ?
    Removing duplicate elements from an array is necessary for data integrity and efficient processing. The approaches implemented and explained below will use the Lodash to remove duplicate elements from an array. Table of Content Using uniq methodUsing groupBy and map methodsUsing xor functionUsing un
    3 min read
  • Remove Duplicate Elements from TypeScript Array
    TypeScript array is a single variable that is used to store the elements or a group of values of the same type. An array can contain multiple occurrences or duplicates of an element that can be removed using the below-discussed methods. Below are the approaches used to remove Duplicate Elements from
    5 min read
  • Remove Duplicate Elements from JavaScript Array
    To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r
    3 min read
  • How to Remove Empty String from Array in Ruby?
    In this article, we will learn how to remove empty strings from an array in Ruby. We can remove empty strings from an array in Ruby using various methods. Table of Content Remove empty string from array using rejectRemove empty string from array using selectRemove empty string from array using map a
    2 min read
  • Golang Program that Removes Duplicate Elements From the Array
    Arrays in Golang or Go programming language is much similar to other programming languages. In the program, sometimes we need to store a collection of data of the same type, like a list of student marks. Such type of collection is stored in a program using an Array. An array is a fixed-length sequen
    4 min read
  • Remove array elements in Ruby
    In this article, we will learn how to remove elements from an array in Ruby. Method #1: Using Index C/C++ Code # Ruby program to remove elements # in array # creating string using [] str = ["GFG", "G4G", "Sudo", "Geeks"] str.delete_at(0) print str Output: ["G4
    1 min read
  • How to remove all nil elements from an Array in Ruby permanently?
    In this article, we will discuss how to remove all nil elements from an array permanently in Ruby. We can remove all nil elements from an array permanently through different methods ranging from using compact! method to delete method with nil argument. Table of Content Removing all nil elements from
    2 min read
  • How to delete an Element From an Array in PHP ?
    To delete an element from an array means to remove a specific value or item from the array, shifting subsequent elements to the left to fill the gap. This operation adjusts the array's length accordingly, eliminating the specified element. This article discusses some of the most common methods used
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences