AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 355 of 840

Checking for increasing triplet in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 327 Views

In JavaScript, an increasing triplet refers to three numbers in an array where each number is greater than the previous one, appearing in sequence (not necessarily consecutive positions). Understanding Increasing Sequences A sequence of numbers in which each succeeding element is either greater or equal to the preceding element is an increasing sequence. For instance: 4, 6, 8, 9, 11, 14 is increasing sequence 3, 3, 3, 3, 3, 3, 3 is also an increasing sequence Problem Statement We need to write a JavaScript function that takes an array of numbers and ...

Read More

Finding sum of remaining numbers to reach target average using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 208 Views

Problem We are required to write a JavaScript function that takes in an array of numbers and a single number. Our function should find that very number which should be pushed to the array so that its average equals the number specified by the second argument. Understanding the Formula To find the number that makes the average equal to the target, we use this mathematical approach: Current sum of array elements New array length will be (current length + 1) Required total sum = target × new array length ...

Read More

Breaking a string into chunks of defined length and removing spaces using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 321 Views

We are required to write a JavaScript function that takes in a string sentence that might contain spaces as the first argument and a number as the second argument. Our function should first remove all the spaces from the string and then break the string into a number of chunks specified by the second argument. All the string chunks should have the same length with an exception of last chunk, which might, in some cases, have a different length. Solution Approach The solution involves two main steps: Remove all spaces using ...

Read More

Finding deviations in two Number arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 178 Views

We are required to write a JavaScript function that takes in two number arrays and returns the elements that are not common to both arrays (symmetric difference). For example, if the two arrays are: const arr1 = [2, 4, 2, 4, 6, 4, 3]; const arr2 = [4, 2, 5, 12, 4, 1, 3, 34]; Then the output should be: [6, 5, 12, 1, 34] Method 1: Using indexOf() Method This approach uses nested loops to check if elements exist in the other array: const arr1 = [2, ...

Read More

What happens when length of object is set to 0 - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 421 Views

In JavaScript, setting an array's length property to 0 immediately removes all elements and clears the array. This is an efficient way to empty an array without creating a new one. Initial Array Example Let's start with an array containing some elements: var arrayObject = [ "John", "David", "Mike" ]; console.log("Original array:", arrayObject); console.log("Original length:", arrayObject.length); Original array: [ 'John', 'David', 'Mike' ] Original length: 3 Setting Length to 0 When you set length = 0, ...

Read More

Take in array of objects and convert the above JSON to a Tree-structure in JavaScript

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

In web development, you often need to convert flat arrays of hierarchical data into tree structures. This is common when working with organizational charts, file systems, or nested menus. Input Data Structure Suppose we have an array of objects representing parent-child relationships: const arr = [ { "parentIndex": '0', "childIndex": '3', "parent": "ROOT", "child": "root3" }, ...

Read More

Finding the longest common consecutive substring between two strings in JavaScript

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

We are required to write a JavaScript function that takes in two strings. Let's call them str1 and str2. The function should then find out the longest consecutive string that is common to both the input strings and return that common string. For example − If the input strings are − const str1 = 'ABABC'; const str2 = 'BABCA'; Then the output string should be − const output = 'BABC'; How It Works This problem uses dynamic programming to build a 2D matrix where each cell [i][j] represents the ...

Read More

Adding elements to array to make its sum diverse in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 180 Views

We are required to write a JavaScript function that takes in an array of Numbers, arr, as the first argument and a single number, num, as the second argument. We should, by adding elements to it, make our array such that any sum can be obtained by adding specific numbers from it between [0, num] (including both). Our function should finally return the minimum number of numbers required to add to the array so that it can produce any sum between 0 and num. Problem Example For example, if the input to the function is: ...

Read More

Counting the number of letters that occupy their positions in the alphabets for array of strings using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 361 Views

We are required to write a JavaScript function that takes in an array of strings of English lowercase alphabets. Our function should map the input array to an array whose corresponding elements are the count of the number of characters that had the same 1-based index in the string as their 1-based index in the alphabets. For instance, this count for the string 'akcle' will be 3 because the characters 'a', 'c' and 'e' have 1-based index of 1, 3 and 5 respectively both in the string and the English alphabets. Understanding the Problem We need ...

Read More

How to process JavaScript nested array and display the order of numbers according to the level upto which they are nested?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 324 Views

Processing nested arrays and displaying elements with proper indentation based on their nesting level is a common JavaScript task. This technique helps visualize the hierarchical structure of nested data. Problem Statement Given a nested array like this: const arr = [23, 6, [2, [6, 2, 1, 2], 2], 5, 2]; We need to display numbers with indentation showing their nesting level. Elements at deeper levels should have more indentation. Expected Output 23 6 2 6 2 ...

Read More
Showing 3541–3550 of 8,392 articles
« Prev 1 353 354 355 356 357 840 Next »
Advertisements