AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 332 of 840

Finding the only even or the only odd number in a string of space separated numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 488 Views

We are required to write a JavaScript function that takes in a string that contains numbers separated by spaces. The string either contains all odd numbers and only one even number or all even numbers and only one odd number. Our function should return that one different number from the string. Problem Given a string of space-separated numbers where all numbers are either odd or even except for one, we need to find and return that unique number that differs from the rest. Example Following is the code: const str = '2 4 ...

Read More

Minimum deletion sum of characters in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 180 Views

The minimum deletion sum problem requires finding the lowest ASCII sum of characters that need to be deleted from two strings to make them equal. This is solved using dynamic programming to find the optimal sequence of deletions. Problem Statement Given two strings of lowercase English letters, we need to delete characters from both strings to make them identical, while minimizing the sum of ASCII values of deleted characters. Algorithm Approach We use dynamic programming where each cell dp[i][j] represents the minimum deletion cost to make substrings str1[i:] and str2[j:] equal. Example Input: ...

Read More

How does internationalization work in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 358 Views

In this article, you will understand how internationalization works in JavaScript. Internationalization is the process of preparing software so that it can support local languages and cultural settings. It can include changing the date and time format, changing the metric system format, language format, etc. JavaScript provides the Intl object which contains constructors for locale-sensitive formatting and language-sensitive string comparison. The most commonly used are Intl.DateTimeFormat for dates and Intl.NumberFormat for numbers. Date and Time Formatting Let's understand how to format dates for different locales using Intl.DateTimeFormat: var inputDate = new Date(1990, 2, 25); console.log("The ...

Read More

Sorting the numbers from within in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 198 Views

We are required to write a JavaScript function that takes in an array of numbers and reorders the digits of all the numbers internally in a specific order (let's say in ascending order for the sake of this problem). For example: If the array is − const arr = [543, 65, 343, 75, 567, 878, 87]; Then the output should be − const output = [345, 56, 334, 57, 567, 788, 78]; Therefore, let's write the code for this function − Example The code for this will be − ...

Read More

Algorithm to sum ranges that lie within another separate range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 175 Views

We have two sets of ranges; one is a single range of any length (R1) and the other is a set of ranges (R2) some or parts of which may or may not lie within the single range (R1). We need to calculate the sum of the ranges in (R2) - whole or partial - that lie within the single range (R1). Problem Breakdown For each range in R2, we calculate the overlapping portion with R1 and sum all overlaps: const R1 = [20, 40]; const R2 = [[14, 22], [24, 27], [31, 35], [38, ...

Read More

Building a lexicographically increasing sequence of first n natural numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 173 Views

We are required to write a JavaScript function that takes in a number n and returns an array containing the first n natural numbers. The condition is that the numbers should be sorted lexicographically, which means all numbers starting with 1 should come before any starting with 2, 3, 4, and so on. Understanding Lexicographical Ordering Lexicographical ordering treats numbers as strings. For example, "10" comes before "2" because "1" comes before "2" alphabetically. This creates the sequence: 1, 10, 11, 12, ..., 19, 2, 20, 21, ..., etc. Example Implementation const num = 24; ...

Read More

Sorting array based on increasing frequency of elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 319 Views

We need to write a JavaScript function that sorts an array based on the frequency of elements. Elements with lower frequency appear first, and elements with the same frequency are sorted in ascending order. Problem Given an array that might contain duplicates, we want to sort it so that: Elements appearing fewer times come first Elements with the same frequency are sorted in ascending order For example, if the input is: const arr = [5, 4, 5, 4, 2, 1, 12]; The expected output is: [1, 2, 12, ...

Read More

How does Promise.all() method differs from Promise.allSettled() method in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 275 Views

In this article, you will understand how Promise.all() method differs from the Promise.allSettled() method in JavaScript. The Promise.all() method takes one or multiple promises as input and returns a single Promise. This returned promise fulfills when all of the input promises are fulfilled. It rejects immediately when any of the input promises is rejected, with this first rejection reason. The Promise.allSettled() method takes one or multiple promises as input and returns a single Promise. This returned promise fulfills when all of the input promises settle (either fulfilled or rejected), returning an array of objects that describe the outcome ...

Read More

Constructing array from string unique characters in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 191 Views

We are required to write a JavaScript function that takes in a string and starts mapping its characters from 0. And every time, the function encounters a unique (non-duplicate) character it should increase the mapping count by 1 otherwise it should map the same number for duplicate characters. For example: If the string is − const str = 'heeeyyyy'; Then the output should be − const output = [0, 1, 1, 1, 2, 2, 2, 2]; Therefore, let's write the code for this function − Example The code ...

Read More

Array filtering using first string letter in JavaScript

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

Suppose we have an array that contains names of some people like this: const arr = ['Amy', 'Dolly', 'Jason', 'Madison', 'Patricia']; console.log(arr); [ 'Amy', 'Dolly', 'Jason', 'Madison', 'Patricia' ] We are required to write a JavaScript function that takes in one such array as the first argument, and two lowercase alphabet characters as second and third arguments. Then, our function should filter the array to contain only those elements that start with alphabets that fall within the range specified by the second and third arguments. Therefore, if the second and third arguments ...

Read More
Showing 3311–3320 of 8,392 articles
« Prev 1 330 331 332 333 334 840 Next »
Advertisements