Javascript Articles

Page 257 of 534

Two sum problem in linear time in JavaScript

Sakshi Jain
Sakshi Jain
Updated on 15-Mar-2026 2K+ Views

The Two Sum Problem is a classic algorithmic challenge that asks us to find two numbers in an array that add up to a specific target value. This article demonstrates how to solve this problem efficiently in linear time using JavaScript. What is the Two Sum Problem? The Two Sum Problem requires finding a pair of indices in an array of numbers that add up to a given target sum. Given an array of integers and a target sum, we need to return the indices of two numbers that sum to the target value. The key constraint ...

Read More

Determining whether numbers form additive sequence in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 381 Views

This problem asks us to determine if the given numbers in an array form an additive sequence. An additive sequence is one where each number (starting from the third) equals the sum of the two preceding numbers. Understanding Additive Sequences An additive sequence must have at least three numbers. Each consecutive number in the series, except the first two, must equal the sum of the two numbers before it. // Example: 112358 as a string of digits // 1 + 1 = 2 // 1 + 2 = 3 // 2 + 3 = ...

Read More

Fibonacci like sequence in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 468 Views

In JavaScript, you can generate a Fibonacci-like sequence using various approaches including loops and recursion. The Fibonacci sequence is fundamental in programming and demonstrates different algorithmic techniques. What is the Fibonacci Sequence? The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding numbers. The sequence starts with 0 and 1, then continues as: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Formula: F(n) = F(n-1) + F(n-2), where F(0) = 0 and F(1) = 1 Method 1: Generate Fibonacci Up to a Certain Number ...

Read More

Get value for key from nested JSON object in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 18K+ Views

In JavaScript, accessing values from nested JSON objects is a common task when working with APIs and complex data structures. We can retrieve nested values using dot notation, bracket notation, or custom functions. Before exploring different approaches, let's understand what JSON and nested JSON objects are: What is JSON and Nested JSON Objects? JSON (JavaScript Object Notation) is a lightweight, text-based format for representing structured data. It's widely used for data transmission between web applications and servers. Nested JSON objects contain objects within other objects, creating a hierarchical structure. Each nested property has a unique path ...

Read More

Group matching element in array in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 402 Views

In JavaScript, grouping matching elements in an array is a common task that can be efficiently solved using the reduce() method. This approach allows us to iterate through an array and group consecutive duplicate elements together. What is the reduce() Function? The reduce() method is a built-in JavaScript function that processes each element of an array and accumulates a result. It takes a callback function with two main parameters: an accumulator (which stores the accumulated result) and the current value being processed. Basic reduce() Example const numbers = [1, 2, 3, 4, 5]; const sum ...

Read More

How to implement backtracking for a climbing stairs practice in JavaScript?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 443 Views

In the given problem statement we are asked to implement backtracking for a climbing stairs practice with the help of JavaScript functionalities. In data structures, backtracking is popularly known to explore all possible solutions. We can solve this problem with the help of a backtracking algorithm. What is the Backtracking Technique? Backtracking is a well-known algorithmic technique, which is basically used to solve problem statements that include searching for all possible solutions. It is based on a depth-first strategy and is used in combination with a recursive method. The basic ideology for this technique is to explore ...

Read More

Sorting an array including the elements present in the subarrays in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 309 Views

In JavaScript, we often encounter arrays that contain nested subarrays and need to sort all elements together. This involves flattening the nested structure and then applying sorting algorithms. Understanding the Required Methods Before diving into solutions, let's understand the key JavaScript methods we'll use: sort() method: Sorts array elements in place. By default, it converts elements to strings and sorts alphabetically. For numeric sorting, we provide a comparison function: let numbers = [3, 1, 4, 1, 5, 9, 2, 6]; numbers.sort((a, b) => a - b); // ascending order console.log(numbers); [ 1, ...

Read More

Using BigInt to calculate long factorials in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 2K+ Views

In JavaScript, calculating factorials of large numbers requires the BigInt data type to avoid precision loss. BigInt handles arbitrarily large integers, making it perfect for computing long factorials that exceed JavaScript's standard number limits. What is BigInt? BigInt is a built-in JavaScript data type introduced in ECMAScript 2020 that represents integers of arbitrary size. Unlike the standard Number type, which has a maximum safe integer limit of 2^53 - 1, BigInt can handle infinitely large integers. // Regular numbers have limits console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991 // BigInt can handle much larger values console.log(BigInt(Number.MAX_SAFE_INTEGER) * 100n); ...

Read More

Merging subarrays in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 670 Views

This problem demonstrates how to merge subarrays containing email data by grouping emails under the same person's name. We'll use JavaScript's Set data structure to eliminate duplicates and organize the data efficiently. Understanding the Problem Given an array of subarrays where each subarray contains a person's name followed by their email addresses, we need to merge all emails belonging to the same person into a single subarray. For example, if "Ayaan" appears in multiple subarrays with different emails, we should combine all of Ayaan's emails into one subarray. Algorithm Step 1 − Create a function that ...

Read More

What is the best way to search for an item in a sorted list in JavaScript?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 586 Views

When searching for an item in a sorted array in JavaScript, binary search is the most efficient approach. Unlike linear search which checks every element, binary search takes advantage of the sorted order to eliminate half the search space with each comparison. What is Binary Search? Binary search is a divide-and-conquer algorithm that works on sorted arrays. It compares the target value with the middle element and eliminates half of the remaining elements at each step. This approach has O(log n) time complexity, making it much faster than linear search's O(n). How Binary Search Works The ...

Read More
Showing 2561–2570 of 5,340 articles
« Prev 1 255 256 257 258 259 534 Next »
Advertisements