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
Javascript Articles
Page 70 of 534
Finding sum of all unique elements in JavaScript
In JavaScript, you can sum duplicate elements by counting their occurrences and multiplying each unique value by its count. This technique is useful for consolidating arrays with repeated values. For example, if the input array is: const input = [1, 3, 1, 3, 5, 7, 5, 4]; The output should be: [2, 6, 10, 7, 4] This means: 1 appears 2 times (1×2=2), 3 appears 2 times (3×2=6), 5 appears 2 times (5×2=10), while 7 and 4 appear once each. Using Map to Count and Sum The most efficient ...
Read MoreDeviations in two JavaScript arrays in JavaScript
We have two arrays of numbers and need to find elements that exist in one array but not in both. This is called finding the symmetric difference or deviation between arrays. const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; We need to write a JavaScript function that takes two arrays and returns elements that are not common to both arrays. Using indexOf() Method The basic approach uses indexOf() to check if elements exist in the other array: const arr1 ...
Read MoreAND product of arrays in JavaScript
We have an array of arrays of boolean values like this: const arr = [[true, false, false], [false, false, false], [false, false, true]]; We are required to write a function that merges this array of arrays into a one-dimensional array by combining the corresponding elements of each subarray using the AND (&&) operator. Let's write the code for this function. We will be using Array.prototype.reduce() function to achieve this. Example The code for this will be: const arr = [[true, false, false], [false, false, false], [false, false, true]]; const andMerge ...
Read MoreKeeping only alphanumerals in a JavaScript string in JavaScript
We are required to write a JavaScript function that takes in a string that might contain some special characters. The function should return a new string that has all special characters replaced with their corresponding ASCII value, keeping only alphanumeric characters and spaces in their original form. Therefore, let's write the code for this function: Example The code for this will be: const str = 'Th!s !s @ str!ng th@t cont@!ns some special characters!!'; const specialToASCII = str => { let res = ''; for(let i ...
Read MoreDetecting the first non-unique element in array in JavaScript
We are required to write a function that returns the index of the very first element that appears at least twice in the array. If no element appears more than once, we have to return -1. We have to do this in constant space (i.e., without utilizing extra memory). So, let's write the solution for this problem. We will use a for loop to iterate over the array and use the Array.prototype.lastIndexOf() method to check for duplicacy. How It Works The algorithm works by comparing each element's current index with its last occurrence index using lastIndexOf(). ...
Read MoreInserting empty string in place of repeating values in JavaScript
We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end. For example: If we find 4 duplicate values we have to remove them all and insert four empty strings at the end. Therefore, let's write the code for this function − Example The code for this will be − const arr = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 5, 12, 1, 23, 4, 1]; const deleteAndInsert = arr => { ...
Read MoreCumulative average of pair of elements in JavaScript
We have an array of numbers and need to write a function that returns an array with the average of each element and its predecessor. For the first element, since there's no predecessor, we return the element itself. Let's implement this using the Array.prototype.map() method to transform each element based on its position. How It Works The algorithm works as follows: For the first element (index 0): return the element itself For other elements: calculate (current + previous) / 2 Example const arr = [3, 5, 7, 8, 3, 5, 7, ...
Read MoreExpanding Numerals in JavaScript
We are required to write a function that, given a number, say, 123, will output an array − [100, 20, 3] Basically, the function is expected to return an array that contains the place value of all the digits present in the number taken as an argument by the function. Understanding Place Values Each digit in a number has a place value based on its position. For example, in 123: 1 is in the hundreds place (1 × 100 = 100) 2 is in the ...
Read MoreFiltering array to contain palindrome elements in JavaScript
We are required to write a JavaScript function that takes in an array of String / Number literals and returns a subarray of all the elements that were palindrome in the original array. For example If the input array is − const arr = ['carecar', 1344, 12321, 'did', 'cannot']; Then the output should be − const output = [12321, 'did']; We will create a helper function that takes in a number or a string and checks if it's a palindrome or not. Then we will loop over the array, filter ...
Read MoreSorting a JSON object in JavaScript
In JavaScript, objects themselves cannot be sorted since their properties don't have a guaranteed order. However, we can extract the values from a JSON object and sort them into an array. Suppose we have an object like this: const obj = { key1: 56, key2: 67, key3: 23, key4: 11, key5: 88 }; We need to write a JavaScript function that takes this object and returns a sorted array of its values: const arr = [11, 23, 56, ...
Read More