AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 285 of 840

Filter unique array values and sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 784 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
AmitDiwan
Updated on 15-Mar-2026 337 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
AmitDiwan
Updated on 15-Mar-2026 799 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
AmitDiwan
Updated on 15-Mar-2026 255 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 redundantly remove duplicate elements within an array – JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 328 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
AmitDiwan
Updated on 15-Mar-2026 648 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
AmitDiwan
Updated on 15-Mar-2026 202 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
AmitDiwan
Updated on 15-Mar-2026 187 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

How to set cookie value with AJAX request in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 4K+ Views

When making AJAX requests in JavaScript, you often need to send cookies to the server for authentication or session management. Fortunately, modern browsers automatically include cookies in AJAX requests to the same domain, but you need to set them properly first. How Cookies Work with AJAX AJAX requests automatically send all relevant cookies to the server without additional configuration. The key is setting the cookie correctly using JavaScript's document.cookie property before making your AJAX call. Setting a Basic Cookie Here's how to set a ...

Read More

Switching positions of selected characters in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 447 Views

We are required to write a JavaScript function that takes in a string containing only the letters 'k', 'l' and 'm'. The task is to switch the positions of 'k' with 'l' while leaving all instances of 'm' at their original positions. Problem Statement Given a string with characters 'k', 'l', and 'm', we need to swap every occurrence of 'k' with 'l' and vice versa, keeping 'm' unchanged. Example Here's the implementation using a simple loop approach: const str = 'kklkmlkk'; const switchPositions = (str = '') => { ...

Read More
Showing 2841–2850 of 8,392 articles
« Prev 1 283 284 285 286 287 840 Next »
Advertisements