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
Articles by AmitDiwan
Page 285 of 840
Filter unique array values and sum in JavaScript
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 MoreSum identical elements within one array in JavaScript
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 MoreWhy doesn't Postman get a "No 'Access-ControlAllow-Origin' header is present on the requested resource" error in JavaScript
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 MoreUnique substrings in circular string in JavaScript
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 MoreHow to redundantly remove duplicate elements within an array – JavaScript?
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 MoreConverting array of Numbers to cumulative sum array in JavaScript
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 MoreSumming up unique array values in JavaScript
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 MoreDetermine whether there is a pair of values in the array where the average of the pair equals to the target average in JavaScript
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 MoreHow to set cookie value with AJAX request in JavaScript?
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 MoreSwitching positions of selected characters in a string in JavaScript
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