AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 311 of 840

Chunking arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 209 Views

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 More

Flat array of objects to tree in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 834 Views

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 More

Finding matching pair from an array in JavaScript

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

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 More

Finding minimum flips in a binary string using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 260 Views

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 More

Check if the elements of the array can be rearranged to form a sequence of numbers or not 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 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 More

Flatten array to 1 line in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 291 Views

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 More

Squared and square rooted sum of numbers of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 461 Views

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 More

Binary subarrays with desired sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 160 Views

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 More

How many times can we sum number digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 190 Views

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 More

Greatest number in a dynamically typed array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 182 Views

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
Showing 3101–3110 of 8,392 articles
« Prev 1 309 310 311 312 313 840 Next »
Advertisements