Filter unique array values and sum in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

795 Views

In JavaScript, you may need to filter arrays that contain duplicate values and combine their numeric values. This is common when processing data like product orders, transactions, or inventory records. Problem Statement Given an array of arrays where each subarray has three elements [id, name, amount], we need to: Remove duplicate entries based on the first element (id) Sum the third element (amount) for matching entries const arr = [[12345, "product", "10"], [12345, "product", "15"], [1234567, "other", "10"]]; The expected output should combine the duplicate entries and sum their amounts: ... Read More

Sum identical elements within one array in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

353 Views

We are required to write a JavaScript function that takes in an array of numbers. The array might contain some repeating / duplicate entries within it. Our function should add all the duplicate entries and return the new array thus formed. Example The code for this will be − const arr = [20, 20, 20, 10, 10, 5, 1]; const sumIdentical = (arr = []) => { let map = {}; for (let i = 0; i < arr.length; i++) { ... Read More

Why doesn't Postman get a "No 'Access-ControlAllow-Origin' header is present on the requested resource" error in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

813 Views

Understanding the CORS Difference When making network requests to a remote server with a different origin, web browsers enforce CORS (Cross-Origin Resource Sharing) policies and show "No 'Access-Control-Allow-Origin' header" errors. However, tools like Postman can successfully make the same requests without encountering these CORS restrictions. The Browser Environment Problem Web browsers implement the Same-Origin Policy as a security measure. When JavaScript code running in a browser tries to make a request to a different domain, protocol, or port, the browser blocks the request before it reaches the server: // This will likely cause a ... Read More

Unique substrings in circular string in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

265 Views

In JavaScript, we need to find the number of unique non-empty substrings of a given string that exist in an infinite wraparound string of the alphabet. The wraparound string is an infinite repetition of "abcdefghijklmnopqrstuvwxyz". Problem Statement Given a string S, which is an infinite wraparound string of: "abcdefghijklmnopqrstuvwxyz" The infinite string S looks like: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd...." We need to find how many unique non-empty substrings of a given input string are present in this infinite wraparound string. Example For input string "zab": Input: "zab" Output: 6 ... Read More

How to make a polygon object react to the mouse events using FabricJS?

Rahul Gurung
Updated on 15-Mar-2026 23:19:00

888 Views

We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of a set of connected straight line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. We use the mouseup and mousedown events to demonstrate how a polygon object reacts to the mouse events triggered by a user. Syntax polygon.on("mouseup", callbackFunction); polygon.on("mousedown", callbackFunction); Parameters The on() method accepts two parameters: event - ... Read More

How to change the text and image by just clicking a button in JavaScript?

Abhishek
Updated on 15-Mar-2026 23:19:00

22K+ Views

In JavaScript, you can dynamically change text content and images by using the onclick event with button elements. This allows for interactive web pages where content updates in response to user actions. Let us explore how to change text and images individually using JavaScript with practical examples. Changing Text of an Element JavaScript provides two main properties for modifying element text content: innerText − Changes or retrieves the plain text content of an element, ignoring HTML tags. innerHTML − Changes or retrieves the HTML content of an element, including any HTML tags within it. ... Read More

How to redundantly remove duplicate elements within an array – JavaScript?

AmitDiwan
Updated on 15-Mar-2026 23:19:00

341 Views

Let's say we have an array with duplicate elements like this: [10, 20, 10, 50, 60, 10, 20, 40, 50] JavaScript provides several methods to remove duplicate elements from arrays. The most common and efficient approach is using the Set object with the spread operator. Using Set with Spread Operator (Recommended) The Set object stores only unique values. Combined with the spread operator, it creates a new array without duplicates: var originalArray = [10, 20, 10, 50, 60, 10, 20, 40, 50]; var arrayWithNoDuplicates = [...new Set(originalArray)]; console.log("Original array:", originalArray); console.log("No ... Read More

Converting array of Numbers to cumulative sum array in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

674 Views

We have an array of numbers like this: const arr = [1, 1, 5, 2, -4, 6, 10]; We are required to write a function that returns a new array, of the same size but with each element being the sum of all elements until that point (cumulative sum). Therefore, the output should look like: const output = [1, 2, 7, 9, 5, 11, 21]; Let's explore different approaches to create a cumulative sum array. Using forEach() Method We can iterate through the array and build the cumulative sum by adding each element ... Read More

Summing up unique array values in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

207 Views

We are required to write a JavaScript function that takes in an array of numbers that may contain some duplicate numbers. Our function should return the sum of all the unique elements (elements that only appear once in the array) present in the array. Problem Understanding If the input array is: const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, 11]; The unique elements (appearing only once) are: 3, 7, 4, 11. Their sum is 3 + 7 + 4 + 11 = 25. Using indexOf() and lastIndexOf() We ... Read More

Determine whether there is a pair of values in the array where the average of the pair equals to the target average in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

207 Views

We are required to write a JavaScript function that takes in an array of sorted integers and a target average as first and second arguments. The function should determine whether there is a pair of values in the array where the average of the pair equals to the target average. There's a solution with O(1) additional space complexity and O(n) time complexity. Since an array is sorted, it makes sense to have two indices: one going from begin to end (say y), another from end to begin of an array (say x). Algorithm Approach The two-pointer ... Read More

Advertisements