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 311 of 840
Chunking arrays in JavaScript
Chunking arrays in JavaScript means splitting a single array into smaller subarrays of a specified size. This is useful for pagination, data processing, and organizing information into manageable groups. For example, if we have an array of 7 elements and want chunks of size 2, the last chunk will contain only 1 element since 7 is not evenly divisible by 2. Input and Expected Output Given this input array: const arr = [1, 2, 3, 4, 5, 6, 7]; The expected output should be: [[1, 2], [3, 4], [5, 6], [7]] ...
Read MoreFlat array of objects to tree in JavaScript
Converting a flat array of objects into a tree structure is a common requirement in web development. This involves organizing objects with parent-child relationships into a hierarchical format. The Data Structure We start with a flat array where each object has an id, name, and parentId. Objects with parentId: null are root nodes, while others are children. .parent, .child { cursor: pointer; ...
Read MoreFinding matching pair from an array in JavaScript
We are required to write a JavaScript function that takes in an array of integers that might contain some repeating values. Our function should find out the number of pairs of identical integers we can extract out of the array. For example, if the input array is: const arr = [1, 5, 2, 1, 6, 2, 2, 9]; Then the output should be: const output = 2; because the desired pairs are (1, 1) and (2, 2). Note that we have three 2's, but we can only form one pair from ...
Read MoreFinding minimum flips in a binary string using JavaScript
A monotonically increasing binary string consists of some number of '0's (possibly zero) followed by some number of '1's (also possibly zero). Examples include "000", "111", "0011", or "01". Problem Statement Given a binary string, we need to find the minimum number of flips required to make it monotonically increasing. We can flip any '0' to '1' or any '1' to '0'. For example, with input "00110", we can flip the last '0' to '1' to get "00111", which requires only 1 flip. Approach Using Dynamic Programming We use memoization to track the minimum flips ...
Read MoreCheck if the elements of the array can be rearranged to form a sequence of numbers or not in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and checks if the elements of the array can be rearranged to form a sequence of numbers or not. For example: If the array is − const arr = [3, 1, 4, 2, 5]; Then the output should be true because these numbers can be rearranged to form the consecutive sequence: 1, 2, 3, 4, 5. Approach To solve this problem, we need to: Sort the array in ascending order Check if each element is exactly ...
Read MoreFlatten array to 1 line in JavaScript
Suppose, we have a nested array of numbers like this − const arr = [ [ 0, 0, 0, -8.5, 28, 8.5 ], [ 1, 1, -3, 0, 3, 12 ], [ 2, 2, -0.5, 0, 0.5, 5.3 ] ]; We are required to write a JavaScript function that takes in one such nested array of numbers. The function should combine all the numbers in the nested array to form a single string. In the resulting string, the adjacent numbers should be separated by whitespaces and ...
Read MoreSquared and square rooted sum of numbers of an array in JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers. Our function should take each number in the array and square it if it is even, or square root the number if it is odd and then return the sum of all the new numbers rounded to two decimal places. Example Following is the code − const arr = [45, 2, 13, 5, 14, 1, 20]; const squareAndRootSum = (arr = []) => { const res = arr.map(el => { ...
Read MoreBinary subarrays with desired sum in JavaScript
We need to write a JavaScript function that counts the number of subarrays in a binary array whose elements sum to a target value. Problem Statement Given a binary array arr and a target number, count all subarrays where the sum of elements equals the target. Example Input: const arr = [1, 0, 1, 0, 1]; const target = 2; Expected Output: 4 Explanation: The subarrays with sum 2 are: [1, 0, 1] (indices 0-2) [1, 0, 1] (indices 2-4) [0, 1, 0, 1] (indices 1-4) [1, 0, ...
Read MoreHow many times can we sum number digits in JavaScript
We are required to write a JavaScript function that takes in a positive integer and returns its additive persistence. The additive persistence of an integer, say n, is the number of times we have to replace the number with the sum of its digits until the number becomes a single digit integer. Example Walkthrough For example: If the number is: 1679583 Then we calculate: 1 + 6 + 7 + 9 + 5 + 8 + 3 = 39 // Pass 1 3 + 9 = 12 ...
Read MoreGreatest number in a dynamically typed array in JavaScript
We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some false values. Our function should return the biggest Number from the array. For example: If the input array is − const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; Then the output should be 65. Using Math.max with Array Filtering The most straightforward approach is to filter valid numbers first, then use Math.max(): const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; ...
Read More