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
Front End Technology Articles
Page 374 of 652
Search Element in an Javascript Hash Table
JavaScript hash tables use a get method to search for elements by their key. The method calculates the hash code and searches through the chain at that index to find the matching key-value pair. The get() Method Implementation The search operation involves computing the hash code for the given key and then iterating through the chain at that bucket to find the exact match: get(key) { let hashCode = hash(key); for(let i = 0; i < this.container[hashCode].length; i ++) { ...
Read MoreOdd even sort in an array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and sorts the array such that first all the even numbers appear in ascending order and then all the odd numbers appear in ascending order. For example: If the input array is − const arr = [2, 5, 2, 6, 7, 1, 8, 9]; Then the output should be − const output = [2, 2, 6, 8, 1, 5, 7, 9]; Approach The solution uses a custom comparator function that: Places even numbers ...
Read MoreRemove elements from Javascript Hash Table
To remove elements from a JavaScript hash table, we need to locate the element using its key and remove it from the underlying storage structure. In hash tables that use chaining for collision resolution, this involves searching through the chain at the computed hash index. Let us look at the implementation of the remove method: Remove Method Implementation remove(key) { let hashCode = this.hash(key); for (let i = 0; i < this.container[hashCode].length; i++) { // Find the element in ...
Read MoreDigit distance of two numbers - JavaScript
We are required to write a JavaScript function that takes in two numbers and returns their digit distance. What is Digit Distance? The digit distance of two numbers is the absolute sum of the difference between their corresponding digits from left to right. For example, if the numbers are: 345 678 Then the digit distance calculation will be: |3-6| + |4-7| + |5-8| = 3 + 3 + 3 = 9 Implementation Here's how to calculate digit distance in JavaScript: const num1 = 345; const num2 ...
Read MoreJoining two hash tables in Javascript
Sometimes we need to combine hash tables together using a join function to create a new merged hash table. We'll write a static join method that takes 2 HashTables and creates a new HashTable with all the values. For simplicity, values from the second hash table will override values from the first one if there are duplicate keys. Syntax static join(table1, table2) { // Implementation logic return newHashTable; } Implementation Here's the complete implementation of the join method: class HashTable { ...
Read MoreJavaScript - Find the smallest n digit number or greater
We are required to write a JavaScript function that takes in a number as the first argument, say n, and an array of numbers as the second argument. The function should return the smallest n digit number which is a multiple of all the elements specified in the array. If there exist no such n digit element then we should return the smallest such element. For example: If the array is: const arr = [12, 4, 5, 10, 9] For both n = 2 and n = 3, the output should be 180 Example Following ...
Read MoreThe HashTable Class in Javascript
A HashTable (Hash Map) is a data structure that stores key-value pairs using a hash function to compute array indices. This implementation uses chaining to handle collisions, where multiple values can be stored at the same index using arrays. Complete HashTable Implementation class HashTable { constructor() { this.container = []; // Populate the container with empty arrays // which can be used to add more elements in // cases of collisions for (let i = ...
Read MoreTree Data Structure in Javascript
The tree data structure represents hierarchical relationships like organization charts, file systems, and DOM elements. A tree consists of nodes connected in a parent-child relationship, where each node has a value and references to its children, with no duplicate references. Tree Terminology Understanding key tree terms is essential: Root: The top node with no parent Parent: A node that has children Child: A node connected to a parent above it Leaf: A node with no children Height: Maximum depth from root to any leaf Depth: Number of edges from root to a specific node ...
Read MoreNode in Javascript
In JavaScript tree data structures, a node is the fundamental building block that contains data and references to other connected nodes. For binary trees, each node has three essential properties: the data it stores and references to its left and right child nodes. Node Structure A typical tree node contains the following components: data − Stores the actual value or information in the node left − Reference to the left child node right − Reference to the right child node Creating a Node Class ...
Read MoreCreating a BinaryTree using Javascript
A Binary Search Tree (BST) is a hierarchical data structure where each node has at most two children. Let's learn how to create and represent a binary search tree in JavaScript by building a complete BST class with essential operations. Basic Structure We'll start by creating the BinarySearchTree class and defining a Node class for individual tree elements. class BinarySearchTree { constructor() { // Initialize a root element to null this.root = null; ...
Read More