AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 466 of 840

Checking for co-prime numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 528 Views

Two numbers are said to be co-primes if there exists no common prime factor amongst them (1 is not a prime number). For example: 4 and 5 are co-primes 9 and 14 are co-primes 18 and 35 are co-primes 21 and 57 are not co-prime because they have 3 as the common prime factor We are required to write a function that takes in two numbers and returns true if they are co-primes otherwise returns false. Understanding Co-prime Numbers Two numbers are co-prime if their greatest common divisor (GCD) is 1. This means ...

Read More

How to get all unique values in a JavaScript array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 538 Views

There are multiple ways to get unique values from a JavaScript array. The most common approaches use Set, filter(), or reduce() methods. Using Set (Recommended) The Set object only stores unique values, making it the simplest approach: Unique Values with Set let arr = [2, 3, 4, 2, 3, 4, "A", "A", "B", "B"]; console.log("Original array:", ...

Read More

How to get the maximum count of repeated letters in a string? JavaScript

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

We have a string that contains some repeated letters like this: const a = "fdsfjngjkdsfhhhhhhhhhhhfsdfsd"; Our job is to write a function that returns the count of maximum consecutive same letters in a streak. Like in the above string the letter 'h' appears for 11 times in a row consecutively, so our function should return 11 for this string. This problem is a good candidate for the sliding window algorithm, where a stable window contains consecutive identical letters and one that contains different elements is unstable. The window adjusts by moving the start pointer when different characters ...

Read More

How to put variable in regular expression match with JavaScript?

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

You can put a variable in a regular expression match by using the RegExp constructor, which accepts variables as patterns. This is essential when you need dynamic pattern matching in JavaScript. Syntax // Using RegExp constructor with variable let pattern = new RegExp(variableName, flags); string.match(pattern); // Or directly with match() string.match(variableName); // for simple string matching Example: Using Variable in Regular Expression let sentence = 'My Name is John'; console.log("The actual value:"); console.log(sentence); let matchWord = 'John'; console.log("The matching value:"); console.log(matchWord); // Using RegExp constructor with variable let matchRegularExpression = ...

Read More

Repeat even number inside the same array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 155 Views

We are required to write a JavaScript function that should repeat the even number inside the same array. Therefore, for example given the following array − const arr = [1, 2, 5, 6, 8]; We should get the output − const output = [1, 2, 2, 5, 6, 6, 8, 8]; Example Following is the code − const arr = [1, 2, 5, 6, 8]; const repeatEvenNumbers = arr => { let end = arr.length - 1; for(let i = ...

Read More

Explain import "as" and Export "as" constructs in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 263 Views

Import as and export as constructs allow you to rename modules during import/export operations, providing flexibility and avoiding naming conflicts in JavaScript. What is Import "as"? The import as syntax allows you to import a named export under a different name in the importing module. This is useful when you want to avoid naming conflicts or use more descriptive names. What is Export "as"? The export as syntax allows you to export a function or variable under a different name than its original declaration. This gives you control over how your module's API is exposed. ...

Read More

map() array of object titles into a new array based on other property value JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 288 Views

Let's say, we have an array of objects like this − const arr = [{ country: "canada", count: 2 }, { country: "jamaica", count: 2 }, { country: "russia", count: 1 }, { country: "india", count: 3 }, { country: "spain", count: 2 }, { country: "portugal", count: 1 }, ...

Read More

Fetch Second minimum element from an array without sorting JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 547 Views

We have an array of Numbers, and we are required to write a function that returns the second smallest value from the array. For example − if the array is − const arr = [67, 87, 56, 8, 56, 78, 54, 67, 98, 56, 54]; Then the output should be the following − 54 because 54 is the smallest value after 8 Method 1: Using indexOf and splice This approach finds the minimum element, removes it from a copy of the array, then finds the minimum of the remaining ...

Read More

Find closest index of array in JavaScript

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

When working with arrays in JavaScript, you might need to find the index of the element that is numerically closest to a given target value. This is useful in scenarios like finding the nearest data point, closest price match, or similar proximity-based searches. Problem Statement Given an array of numbers and a target value, we need to find the index of the array element that has the smallest absolute difference from the target. const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362]; For example, if our target is 150, we ...

Read More

Breaking a loop in functional programming JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

In traditional loops, we use break to exit early. In functional programming, JavaScript provides array methods like some() and every() that can terminate iteration based on conditions. Using Array.some() to Break Early The some() method stops iterating when the callback returns true, making it perfect for early termination: Breaking Loop Example body { font-family: ...

Read More
Showing 4651–4660 of 8,392 articles
« Prev 1 464 465 466 467 468 840 Next »
Advertisements