A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/sets-in-javascript below:

Set in JavaScript - GeeksforGeeks

Set in JavaScript

Last Updated : 13 Aug, 2025

A Set in JavaScript is used to store a unique collection of items, meaning no duplicates are allowed.

JavaScript
// using an array
let s1 = new Set([10, 30, 30, 40, 40]);
console.log(s1);
let s2 = new Set(["gfg", "gfg", "geeks"]);
console.log(s2);

// using string
let s3 = new Set("fooooooood");
console.log(s3);

// an empty set
let s4 = new Set();
console.log(s4);

Output
Set(3) { 10, 30, 40 }
Set(2) { 'gfg', 'geeks' }
Set(3) { 'f', 'o', 'd' }
Set(0) {}
How Set Internally Works in JavaScript

In JavaScript, hash tables are generally used to implement the property of unique values in the Set data structure. The core property of a Set is that it stores only unique values, and any duplicate values are automatically ignored.

Process of Storing Data in a Hash Table
  1. Iteration Over Values: The first step involves iterating over all the values passed in an array, which is then passed to the Set constructor function. This iterator ensures that each value is processed individually.
  2. Hashing with a Hash Function: A hash function, denoted as H(), is introduced. This function takes the value and converts it into a numerical index. The hash function’s purpose is to assign each unique value a specific index in the table.
  3. Mapping Values to Hash Table: In this step, the respective indices for each value are mapped onto the hash table. For example, if the value 1 produces index 5 from the hash function, the value 1 will be stored at index 5 in memory.
  4. Handling Duplicates: When a duplicate value is attempted to be added to the Set, the hash function produces the same index for that value. The algorithm then checks whether that index already contains a value. If the index is already occupied, the new value is discarded, and the previous value is retained, ensuring the uniqueness of values in the Set.
Value Retrieval from the Hash Table

In a hash table, each unique value is stored at a separate index. To retrieve a value from the hash table, the index must be known beforehand. This allows constant-time (O(1)) retrieval since the index is directly used to access the value.

Hash tables are considered optimized and fast for storing data, ensuring efficient management of the Set data structure's functionality, such as maintaining unique values and enabling quick lookups.

Key Characteristics of Sets Methods of Set in JavaScript

JavaScript’s Set object comes with several built-in methods to add, remove, check, and iterate over values. Below are the most commonly used methods with syntax and examples.

Set.add() :

Adds the new element with a specified value at the end of the Set object.

Syntax :

 mySet.add(value);

Example : In this example, we have added two numbers 23 and 12 into the set.

JavaScript
// Create a new set using Set() constructor
let myset = new Set();

// Append new elements to the 
// set using add() method
myset.add(23);
myset.add(12);

// Print the modified set
console.log(myset);

Set.delete() :

deletes an element with the specified value from the Set object. 

Syntax :

mySet.delete(value);

Example : Removing Elements from a Set with the delete() Method.

JavaScript
// Create a new set using Set() constructor
let myset = new Set();

// Append new elements to the set
// using add() method
myset.add(75);
myset.add(12);

// Print the modified set
console.log(myset);

// As 75 exists, it will be removed 
// and it will return true
console.log(myset.delete(75));
console.log(myset);

Output
Set(2) { 75, 12 }
true
Set(1) { 12 }

Set.clear() :

The Set clear() method in JavaScript is used for the removal of all the elements from a set and making it empty.

Syntax:

mySet.clear();

Example : Emptying set using Set clear() method.

JavaScript
// Create a new set using Set() constructor
let myset = new Set();

// Append new elements to the
// set using add() method
myset.add(23);

// Print the modified set
console.log(myset);
console.log(myset.size);

// The clear() method will remove 
// all elements from the set 
myset.clear();

// This will return 0 as there
// are no elements present in the Set
console.log(myset.size);

Set.entries() :

Returns an iterator object which contains an array having the entries of the set, in the insertion order. 

Syntax :

mySet.entries()

Example :

JavaScript
let myset = new Set();

// Adding new elements to the set
myset.add("California");
myset.add("Seattle");
myset.add("Chicago");

// Creating an iterator object
const setIterator = myset.entries();

// Getting values with iterator
console.log(setIterator.next().value);
console.log(setIterator.next().value);
console.log(setIterator.next().value);

Output
[ 'California', 'California' ]
[ 'Seattle', 'Seattle' ]
[ 'Chicago', 'Chicago' ]

Set.has() :

Returns true if the specified value is present in the Set object. 

Syntax :

mySet.has(value);

Example : In this example, we have used sethas() method.

JavaScript
// Create a new set using Set() constructor
let myset = new Set();

// Append new elements to the 
// set using add() method
myset.add(23);
myset.add(12);

// As 23 exists 23, it will return true
console.log(myset.has(23));

Set.values() :

Returns all the values from the Set in the same insertion order.

Syntax:

mySet.values()

Example :

JavaScript
let myset = new Set();

// Adding new element to the set
myset.add("California");
myset.add("Seattle");
myset.add("Chicago");

// Creating a iterator object
const setIterator = myset.values();

// Getting values with iterator
console.log(setIterator.next().value);
console.log(setIterator.next().value);
console.log(setIterator.next().value);

Output
California
Seattle
Chicago

Set.keys() :

Also returns all the values from the Set in the insertion order. 

Syntax :

keys()

Example : In this example, we will see the use of keys in the set.

JavaScript
let placeSet = new Set();

placeSet.add("America");
placeSet.add("England");
placeSet.add("Chicago");

const setIterator = placeSet.keys();

console.log(setIterator);

Output
[Set Iterator] { 'America', 'England', 'Chicago' }

Set.forEach() :

executes the given function once for every element in the Set, in the insertion order.

Syntax :

forEach(function(value, key, set) { 
/* ... */
}, thisArg)

Example : In this example, we will see the use of the forEach() method.

JavaScript
function setValue(value1, value2, mySet) {
    console.log(`s[${value1}] = ${value2}`);
}

new Set(['Chicago', 'California', undefined])
    .forEach(setValue);

Output
s[Chicago] = Chicago
s[California] = California
s[undefined] = undefined
Also Check

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4