Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 399 of 801
Adding two Sets in Javascript
The operation of adding 2 sets is known as a union. You need to add every element from one set to another while checking for duplicates. JavaScript's built-in Set class doesn't include a union method, but we can implement it using several approaches. Method 1: Using Spread Operator (Recommended) The most concise way to combine two sets is using the spread operator: let setA = new Set([1, 2, 3, 4]); let setB = new Set([2, 3, 5, 6]); // Create union using spread operator let unionSet = new Set([...setA, ...setB]); console.log(unionSet); console.log("Size:", unionSet.size); ...
Read MorePeeking elements from a PriorityQueue using JavaScript
Peeking a PriorityQueue means getting the element with the highest priority without removing it from the queue. This operation is useful when you need to inspect the next item to be processed without actually processing it. The peek() Method Implementation The peek function returns the element with the highest priority. In our implementation, elements are stored in ascending order of priority, so the last element has the highest priority: peek() { if (this.isEmpty()) { console.log("Queue Underflow!"); ...
Read MoreSubtract two Sets in Javascript
Set subtraction (difference) removes all elements from the first set that exist in the second set. JavaScript doesn't provide a built-in difference method, but we can create one using forEach or modern Set methods. Using forEach Method We can create a static method to subtract one Set from another: Set.difference = function(s1, s2) { if (!(s1 instanceof Set) || !(s2 instanceof Set)) { console.log("The given objects are not of type Set"); return null; ...
Read MoreThe PriorityQueue Class in Javascript
A Priority Queue is a data structure where each element has an associated priority. Elements with higher priority are served before elements with lower priority. In JavaScript, we can implement this using a class-based approach. Complete PriorityQueue Implementation Here's a complete implementation of the PriorityQueue class with all essential methods: class PriorityQueue { constructor(maxSize) { // Set default max size if not provided if (isNaN(maxSize)) { maxSize = 10; ...
Read MoreAdd elements to a Dictionary in Javascript
In JavaScript, there are multiple ways to add elements to a dictionary-like structure. You can use plain objects, custom dictionary classes, or the ES6 Map object. Using Plain Objects The simplest approach is using JavaScript objects as dictionaries. You can add key-value pairs using bracket notation or dot notation: const dictionary = {}; // Using bracket notation dictionary["key1"] = "value1"; dictionary["key2"] = "value2"; // Using dot notation (for valid identifiers) dictionary.key3 = "value3"; console.log(dictionary); { key1: 'value1', key2: 'value2', key3: 'value3' } Custom Dictionary Class You can ...
Read MoreBasic Operations supported by a list in Javascript
JavaScript arrays (lists) support several fundamental operations that allow you to manipulate and access data efficiently. Here are the core operations you can perform on arrays. Basic List Operations Insertion − Add elements at the beginning, middle, or end of the list Deletion − Remove elements from any position in the list Display − Show the complete list contents Search − Find elements using specific values or conditions Access − Retrieve elements by their index position Insertion Operations You ...
Read MoreClearing a Dictionary using Javascript
In JavaScript, dictionaries can be implemented using plain objects or ES6 Maps. Both approaches provide methods to clear all key-value pairs from the container. Clearing Custom Dictionary Objects For custom dictionary implementations using plain objects, you can create a clear() method that resets the container: clear() { this.container = {} } Example with Custom Dictionary class MyMap { constructor() { this.container = {}; } ...
Read MoreCreating a linked list using Javascript
A linked list is a dynamic data structure where elements (nodes) are stored in sequence, with each node containing data and a reference to the next node. Let's build a complete linked list implementation in JavaScript. Basic Structure We'll start by defining a LinkedList class and a Node structure: class LinkedList { constructor() { this.head = null; this.length = 0; } } LinkedList.prototype.Node = class { constructor(data) ...
Read MoreAdd elements to a linked list using Javascript
In JavaScript, adding elements to a linked list requires careful pointer manipulation to maintain the list structure. We need to create a function insert(data, position) that inserts data at the specified position. Implementation Steps Create a new Node with the provided data Check if the list is empty. If so, add the node as head and return Iterate to the desired position using a current element pointer Update the new node's next pointer to point to the current node's next Update the current node's next pointer to point to the new node Visual Representation ...
Read MoreRemove elements from a linked list using Javascript
Removing an element from a linked list involves breaking the connections between nodes. There are three main scenarios to handle based on the position of the element to be removed. Three Cases for Element Removal Removing from head: Simply assign head = head.next to lose reference to the first element. Removing from tail: Set the second-to-last node's next property to null. Removing from middle: Connect the previous node directly to the node after the one being removed: prevNode.next = nodeToRemove.next. Visual Illustration Original List: ...
Read More