AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 365 of 840

JavaScript function that generates all possible combinations of a string

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 852 Views

We are required to write a JavaScript function that takes in a string as the only argument. The function should generate an array of strings that contains all possible combinations of characters from the string. Understanding the Problem The function uses a bitwise approach to generate all possible combinations. It first extracts individual characters, then uses binary representation to determine which characters to include in each combination. Example Following is the code − const str = 'Delhi'; const allCombinations = (str1 = '') => { const arr = []; ...

Read More

Finding minimum absolute difference within a Binary Search Tree in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 270 Views

We are required to write a JavaScript function that takes in the root of a BST that holds some numerical data like this: 1 \ 3 / 2 The function should return the minimum absolute difference between any two nodes of the tree. For the above tree, the output should be: const output = 1; because |1 - 2| = |3 - 2| = 1 Understanding ...

Read More

Uneven sorting of array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 273 Views

We need to write a JavaScript function that sorts an array in a zigzag pattern where elements alternate between smaller and larger values: arr[0] < arr[1] > arr[2] < arr[3]... Problem Statement Given an array of numbers, we want to rearrange it so that: Even indices (0, 2, 4...) have smaller values Odd indices (1, 3, 5...) have larger values The pattern creates a "wave" or zigzag effect For example, if the input array is [1, 5, 1, 1, 6, 4], a valid output could be [1, 6, 1, 5, 1, 4]. Solution Approach ...

Read More

Counting rings in letters using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 260 Views

We need to write a JavaScript function that counts the number of rings (closed loops) present in letters of a string. Different letters contain different numbers of rings based on their visual structure. Understanding Letter Rings Letters with rings are those that contain closed loops in their visual representation: One ring: A, D, O, P, Q, R, a, b, d, e, g, o, p, q Two rings: B (has two closed loops) No rings: All other letters Example Implementation const str = 'some random text string'; function countRings(str) { ...

Read More

Sorting and find sum of differences for an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 408 Views

Problem We need to write a JavaScript function that takes an array of integers, sorts them in descending order, and then calculates the sum of differences between consecutive pairs. For example, if the array is: [6, 2, 15] After sorting in descending order: [15, 6, 2] The sum of differences would be: (15 - 6) + (6 - 2) = 9 + 4 = 13 Solution Here's the implementation that sorts the array and calculates the sum of consecutive differences: const arr = [6, 2, 15]; ...

Read More

Recursive string parsing into object - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 600 Views

Recursive string parsing transforms an array of dot-separated strings into a nested JavaScript object structure. This technique is useful for converting flat configuration paths into hierarchical data structures. Problem Description Given an array of strings following the pattern x.y.x.y..., we need to create a nested object where the last segment becomes a value in an array, and preceding segments form the nested structure. For example, if the input array is: const arr = [ "country.UK.level.1", "country.UK.level.2", "country.US.level.1", "country.UK.level.3" ...

Read More

Hyphen string to camelCase string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 245 Views

Converting hyphen-separated strings to camelCase is a common requirement in JavaScript development. We need to transform strings like 'this-is-an-example' into 'thisIsAnExample'. Problem Statement Given a string with words separated by hyphens: const str = 'this-is-an-example'; We need to convert it into camelCase format: const output = 'thisIsAnExample'; Using split(), map(), and join() Method The most straightforward approach splits the string by hyphens, capitalizes each word except the first, and joins them back: const str = 'this-is-an-example'; const changeToCamel = str => { ...

Read More

How to sort array by first item in subarray - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 588 Views

In JavaScript, you can sort an array of subarrays based on the first element of each subarray using the sort() method with a custom comparison function. The Problem Consider an array where each element is itself an array, and you want to sort by the first item in each subarray: var studentDetails = [ [89, "John"], [78, "Mary"], [94, "Alice"], [47, "Bob"], [33, "Carol"] ]; Sorting in Descending Order To sort by the ...

Read More

Return Vowels in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 843 Views

We are required to write a JavaScript function that takes in a string that might contain some alphabets. The function should count and return the number of vowels that exists in the string. Syntax function countVowels(str) { // Convert to lowercase for case-insensitive comparison // Loop through each character // Check if character is a vowel (a, e, i, o, u) // Return the count } Example: Using for Loop Following is the code − const ...

Read More

Finding the third maximum number within an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 258 Views

Finding the third maximum number in a JavaScript array requires handling duplicates and edge cases where fewer than three unique values exist. The task is to return the third largest unique number from an array. If fewer than three unique numbers exist, return the maximum number instead. Problem Example For the input array: [34, 67, 31, 87, 12, 30, 22] The unique numbers sorted in descending order are: [87, 67, 34, 31, 30, 22, 12] The third maximum is 34. Solution Approach The algorithm works by: ...

Read More
Showing 3641–3650 of 8,392 articles
« Prev 1 363 364 365 366 367 840 Next »
Advertisements