Javascript Articles

Page 142 of 534

Finding missing element in an array of numbers in JavaScript

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

We are required to write a JavaScript function that takes in an array of numbers of length, say n. The array contains all the integers from 0 to n (including both 0 and n), but just one integer is missing, it can be any number and the array is not sorted. The task of our function is to find the missing number and return it in linear time and constant space. Since the array contains all the numbers from 0 to n but one, we can simply calculate the sum of all the elements of the array in linear ...

Read More

Similar string groups in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 458 Views

Two strings are similar if we can swap exactly two characters at different positions to make them equal, or if they are already equal. Given an array of strings (all anagrams of each other), we need to find how many groups of similar strings exist. For example, "tars" and "rats" are similar (swap positions 0 and 2), and "rats" and "arts" are similar. This forms one group: {"tars", "rats", "arts"}. The string "star" forms its own group since it's not similar to any other string. Understanding Similarity Two strings are similar if: They are identical, or ...

Read More

Splitting array of numbers into two arrays with same average in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 285 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. The function needs to determine whether there exists a combination of elements of the input array that when they are divided into two groups (may/may not have equal elements), the average of both the groups is just the same. If there exists any such condition the function should return true, false otherwise. For example − If the input array is − const arr = [6, 3, 2, 8, 1, 5, 7, 4]; ...

Read More

Armstrong number within a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 490 Views

Armstrong Numbers: A positive integer is called an Armstrong number (of order n) if − abcd... = a^n + b^n + c^n + d^n + ... where n is the number of digits. For example, 153 is an Armstrong number because 1³ + 5³ + 3³ = 1 + 125 + 27 = 153. We are required to write a JavaScript function that takes in an array of exactly two numbers specifying a range. The function should return an array of all the Armstrong numbers that falls in that range (including the start and end ...

Read More

Pair of similar elements at different indices in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 141 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. The function is required to count the number of all such element pairs from the array that are equal in magnitude but are present at different indices. For example, if the input array is: const arr = [7, 9, 5, 7, 7, 5]; Then the output should be: 4 because the desired pairs are [7, 7], [7, 7], [7, 7], [5, 5] How It Works The ...

Read More

Counting substrings of a string that contains only one distinct letter in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 249 Views

We are required to write a JavaScript function that takes in a string as the only argument. The task of our function is to count all the contiguous substrings in the input string that contains exactly one distinct letter. The function should then return the count of all such substrings. For example, if the input string is 'iiiji', then the output should be 8 because the desired substrings are: 'i', 'i', 'i', 'ii', 'ii', 'iii', 'j', and 'i'. How It Works The algorithm identifies consecutive groups of identical characters and counts all possible contiguous substrings within ...

Read More

Large to Small Sorting Algorithm of already sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 374 Views

Suppose we have an array of integers that is already sorted in the increasing order. We are required to write a JavaScript function that without using the inbuilt Array.prototype.sort() method sorts the array like the following − First number should be the maximum Second number should be the minimum Third number should be the 2nd ...

Read More

Finding the elements of nth row of Pascal's triangle in JavaScript

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

Pascal's triangle is a triangular array constructed by summing adjacent elements in preceding rows. Each row starts and ends with 1, and each interior element is the sum of the two elements above it. The first few rows of Pascal's triangle are: 1 1 1 1 2 1 1 3 3 1 ...

Read More

Forming and matching strings of an array based on a random string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 349 Views

In JavaScript, we can check if strings from an array can be formed using characters from a given string. This involves counting character frequencies and matching them against each array element. Problem Statement Given an array of strings and a random string, find the first string from the array that can be completely formed using the characters available in the random string. const arr = ['Dinesh', 'Mahesh', 'Rohit', 'Kamal', 'Jatin Sapru', 'Jai']; const str = 'lsoaakjm'; console.log("Array:", arr); console.log("Available characters:", str); Array: [ 'Dinesh', 'Mahesh', 'Rohit', 'Kamal', 'Jatin Sapru', 'Jai' ] Available ...

Read More

Largest product of n contiguous digits of a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 216 Views

We are required to write a JavaScript function that takes in two numbers as first and the second argument, let us call them m and n. The first number will generally be a number with multiple digits and the second number will always be smaller than the number of digits in the first number. The function should find the group of n consecutive digits from m whose product is the greatest. For example, if the input numbers are: const m = 65467586; const n = 3; Then the output should be: ...

Read More
Showing 1411–1420 of 5,340 articles
« Prev 1 140 141 142 143 144 534 Next »
Advertisements