Javascript Articles

Page 253 of 534

Maximum sum of n consecutive elements of array in JavaScript

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

In JavaScript, finding the maximum sum of n consecutive elements in an array is a classic sliding window problem. This technique efficiently calculates the maximum sum by maintaining a window of fixed size and sliding it across the array. Understanding the Problem The goal is to find a continuous subarray of length n within the given array that has the highest possible sum. For example, given array [1, 2, 4, 7, 3, 5] and n = 3, the consecutive elements [4, 7, 3] have the maximum sum ...

Read More

Non-negative set subtraction in JavaScript

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

In JavaScript, non-negative set subtraction involves removing elements from one set that exist in another set, while ensuring the result contains only non-negative values. This operation combines set theory with value filtering using JavaScript's built-in Set object and forEach method. What is the Set Object? The Set is a built-in data structure introduced in ES6 that stores unique values of any type. Unlike arrays, Sets automatically eliminate duplicates and maintain insertion order. Key characteristics include: Uniqueness: Only unique elements are stored ...

Read More

Pair whose sum exists in the array in JavaScript

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

In this article we will see how to find pairs of items in an array whose sum equals another element present in the same array. We'll use JavaScript to implement an efficient algorithm for this problem. Understanding the Problem The problem is to find pairs of numbers (a, b) in an array where a + b = c, and c is also present in the array. For example, in array [1, 2, 3, 4, 5], the pair (1, 2) has sum 3, which exists in the array. Approach Using Hash Set We'll use a Set data ...

Read More

Parse and balance angle brackets problem in JavaScript

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

In JavaScript, determining whether angle brackets in a string are properly balanced is a common problem when working with HTML, XML, or template parsing. This problem requires checking that every opening angle bracket '' in the correct order. What is a Stack-based Approach? The stack-based approach uses a Last In, First Out (LIFO) data structure to track opening brackets. When we encounter an opening bracket, we push it onto the stack. When we find a closing bracket, we check if there's a matching opening bracket at the top of the stack and pop it if found. Think ...

Read More

Partial sum in array of arrays JavaScript

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

In the given problem statement our task is to get the partial sum in an array of arrays with the help of JavaScript functionalities. The partial sum at position (i, j) represents the sum of all elements from the top-left corner (0, 0) to the current position (i, j). Understanding the Problem An array of arrays (2D array) is a data structure where each element is itself an array. For example: [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] The partial ...

Read More

Pick out numbers from a string in JavaScript

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

In JavaScript, extracting numbers from strings is a common task that can be efficiently accomplished using regular expressions and built-in string methods. This approach allows you to find and extract numeric values from mixed content. Understanding the Problem The goal is to extract all numeric values from a given string. For example, from the string "I am 25 years old and earn $1500.50", we want to extract [25, 1500.50]. This involves pattern matching to identify numeric sequences including integers and decimal numbers. Using Regular Expressions with match() ...

Read More

Remove duplicate items from an array with a custom function in JavaScript

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

In JavaScript, arrays often contain duplicate values that need to be removed. This article demonstrates how to create a custom function to eliminate duplicates and return an array with unique elements only. Understanding the Problem We need to create a function that accepts an array as input and returns a new array containing only unique items, excluding any duplicates. For example, given the array [0, 0, 2, 2, 3, 3], the function should return [0, 2, 3]. Algorithm Overview The algorithm iterates through the input array and adds items to a result array only if they ...

Read More

Retrieve key and values from object in an array JavaScript

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

In JavaScript, extracting keys and values from objects within an array is a common task when processing data structures. This can be accomplished using built-in Object methods like Object.keys() and Object.values(). Understanding the Problem When working with arrays of objects, you often need to access both the property names (keys) and their corresponding values. JavaScript objects are collections of key-value pairs, and the Object class provides methods to extract this information efficiently. Using Object.keys() and Object.values() The Object.keys() method returns an array of property names, while Object.values() returns an array of property values from an object. ...

Read More

Shift strings Circular left and right in JavaScript

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

The main objective for the problem statement is to perform a circular shift on the strings in Javascript. The circular shifts can be left shift or right shift. And implement this solution in Javascript. Understanding the Problem The problem at hand is to shift the strings circularly left and right with the help of Javascript functionalities. Circular shifting means we have to move the characters of the given string in a circular manner in which the characters that are to be shifted beyond the boundaries of the string should reappear at the opposite end. Logic for the ...

Read More

Shortest distance between objects in JavaScript

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

In JavaScript, calculating the shortest distance between two objects requires their coordinates and the Euclidean distance formula. This is commonly used in game development, mapping applications, and geometric calculations. Understanding the Problem To find the shortest distance between two objects, we need their position coordinates (x, y). The distance is calculated using the Euclidean distance formula: √[(x₂-x₁)² + (y₂-y₁)²] The Distance Formula The Euclidean distance formula calculates the straight-line distance between two points in a coordinate system: ...

Read More
Showing 2521–2530 of 5,340 articles
« Prev 1 251 252 253 254 255 534 Next »
Advertisements