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
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Set in JavaScript
Next article icon

Applications of Set in JavaScript

Last Updated : 01 Aug, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

A Set is a collection of items that are unique i.e. no element can be repeated. Set in ES6 are ordered: elements of the set can be iterated in the insertion order. The set can store any type of value whether primitive or objects.

Application of Set in JavaScript:

  • Deleting duplicates element from an array
  • Creating a unique array from the unique values of two arrays
  • Union of two sets
  • Interaction of two sets

Approach 1: Deleting duplicates element from an array

In this approach, we are given a set with multiple duplicate values and we will remove the duplicates from the set.

Example:

JavaScript
// Create an array containing duplicate items const duplcateArray = [     2, 3, 4, 8, 8, 9, 9, 4, 2,      3, 3, 4, 6, 7, 5, 32, 3, 4, 5 ];  // Use Set and Spread Operator to remove  // duplicate items from an array console.log([...new Set(duplcateArray)]); 

Output:

[ 2, 3, 4,  8, 9, 6, 7, 5, 32 ]

Approach 2: Creating a unique array from the unique values of two arrays

In this approach, we will create a unique array from the unique value of two arrays.

Example:

JavaScript
// Declare first array const arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];  // Declare second array const arr2 = [7, 8, 9, 10, 6, 11, 12, 13];  // Function to get the unique values array // from two arrays function uniqueArray(arr1, arr2) {     const set = new Set(arr1.concat(arr2));     return Array.from(set); }  // Display the array containing // unique values console.log(uniqueArray(arr1, arr2)); 

Output
[    1, 2, 3,  4,  5,  6,    7, 8, 9, 10, 11, 12,   13 ]

Approach 3: Union of two sets

In this approach, we will join the values of two sets

Example:

JavaScript
// Create first set let a = new Set([1, 2, 3]);  // Create second set let b = new Set([4, 3, 2]);  // Use spread Operation to Merge both // set and then use Set() constructor // to get union of both set let union = new Set([...a, ...b]);  // Display the result console.log(union) 

Output:

{ 1, 2, 3, 4 }

Approach 4: Interaction of two sets

In this approach, we will get the intersection of two sets.

Example:

JavaScript
// Create first set let a = new Set([1, 2, 3]);  // Create second set let b = new Set([4, 3, 2]);  // Get the instersection of both set let intersection = new Set(     [...a].filter(x => b.has(x)));  // Display the result console.log(intersection) 

Output
Set(2) { 2, 3 }

Next Article
Set in JavaScript

V

vishalkumar2204
Improve
Article Tags :
  • JavaScript

Similar Reads

  • How to write a function in JavaScript ?
    JavaScript functions serve as reusable blocks of code that can be called from anywhere within your application. They eliminate the need to repeat the same code, promoting code reusability and modularity. By breaking down a large program into smaller, manageable functions, programmers can enhance cod
    4 min read
  • Set in JavaScript
    A Set in JavaScript is used to store a unique collection of items, meaning no duplicates are allowed. Sets internally use a hash table which makes search, insert and delete operations faster than arrays. Please note that a hash table data structure allows these operations to be performed on average
    4 min read
  • Set vs Array in JavaScript
    In JavaScript, Set and Array are common data structures used to store collections of data. These data structures are different because of their unique features. Let us look at the implementation of both of these data structures. JavaScript SetSets are used to store collections of unique value withou
    2 min read
  • Set to Array in JS or JavaScript
    This article will show you how to convert a Set to an Array in JavaScript. A set can be converted to an array in JavaScript in the following ways: 1. Using Spread OperatorThe JavaScript Spread Operator can be used to destructure the elements of the array and then assign them to a new variable in the
    2 min read
  • JavaScript Indexed Collections
    Indexed collections in JavaScript refer to data structures like arrays, where elements are stored and accessed by numerical indices. Arrays allow for efficient storage and retrieval of ordered data, providing methods for manipulation and traversal of their elements. Example an array called 'student'
    5 min read
  • Classes and Objects in JavaScript
    Classes Classes were first introduced in the new version of the ES6 classes which replaced the previously used functions. Class is nothing but a blueprint for an object of it. It is used to create an object mainly. If we relate it to a real-life example then it is like a plan for a building or house
    4 min read
  • Addition Assignment (+=) Operator in Javascript
    JavaScript Addition assignment operator(+=) adds a value to a variable, The Addition Assignment (+ =) Sums up left and right operand values and then assigns the result to the left operand. The two major operations that can be performed using this operator are the addition of numbers and the concaten
    2 min read
  • Internal Working of Set in JavaScript
    JavaScript Set is a collection of unique values, i.e. values can not be repeated. The values can be primitive or objects. ES6 sets are ordered according to the insertion order. In this article, we will check the internal working of a set in Javascript. Working of SetSets in JavaScript internally use
    3 min read
  • JavaScript Set has() Method
    The Set.has() method in JavaScript is used to check whether an element with a specified value exists in a Set or not. It returns a boolean value indicating the presence or absence of an element with a specified value in a Set. Syntax:mySet.has(value);Parameters:value: It is the value of the element
    3 min read
  • Interesting Facts about JavaScript Set
    Let us talk about some interesting facts about JavaScript Set that can make you an efficient programmer. Internal WorkingSets internally use hash table which makes set an important choice for problems where we want to store distinct keys with efficient search, insert and delete operations. Please no
    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