Web Development Articles

Page 240 of 801

Finding desired numbers in a sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 262 Views

We have an array of integers which is sorted in the increasing order. We are required to write a JavaScript function that takes in one such array as the first argument and a target sum number as the second argument. The function should find and return two such numbers from the array that when added gives the target sum. The condition for solving this problem is that we have to do this in linear time and using constant space. Two Pointer Approach The optimal solution uses the two-pointer technique. Since the array is sorted, we can place ...

Read More

Finding the first non-repeating character of a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 560 Views

We are required to write a JavaScript function that takes in a string as the first and the only argument. The function should find and return the index of first character it encounters in the string which appears only once in the string. If the string does not contain any unique character, the function should return -1. For example, if the input string is: const str = 'hellohe'; Then the output should be: const output = 4; Because the character 'o' at index 4 is the first character that ...

Read More

Prime digits sum of a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 359 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should then sum all the digits of the number that are prime and return the sum as a number. For example − If the input number is − const num = 67867852; Then the output should be − const output = 21; because 7 + 7 + 5 + 2 = 21 − Understanding Prime Digits Prime digits are single-digit prime numbers: 2, 3, 5, and ...

Read More

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 462 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 288 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 494 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 146 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 257 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 390 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
Showing 2391–2400 of 8,010 articles
« Prev 1 238 239 240 241 242 801 Next »
Advertisements