Web Development Articles

Page 245 of 801

Finding minimum steps to make array elements equal in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 360 Views

We are required to write a JavaScript function that takes in a number, num as the only argument. The function should first construct an array of n elements based on the following rule: arr[i] = (2 * i) + 1; Therefore, if the input number is 5, then the array should be: const arr = [1, 3, 5, 7, 9]; Our function is supposed to calculate and return the minimum number of steps it should take so that all the elements of the array become equal. Understanding the Problem Let ...

Read More

Finding even length numbers from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 540 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 should then construct and return a new array that contains only those elements from the original array that have an even number of digits. For example, if we have an array with numbers of different digit lengths, we need to filter out only those numbers whose digit count is even (2, 4, 6, etc.). Problem Example If the input array is: const arr = [12, 6, 123, 3457, 234, 2]; ...

Read More

Finding average of n top marks of each student in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 654 Views

Suppose, we have an array of objects that contains information about some students and the marks scored by them over a period of time like this: const marks = [ { id: 231, score: 34 }, { id: 233, score: 37 }, { id: 231, score: 31 }, { id: 233, score: 39 }, { id: 231, score: 44 }, { id: 233, score: 41 }, { id: 231, score: ...

Read More

Calculating average of a sliding window in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 693 Views

We are required to write a JavaScript function that takes in an array of integers, arr, as the first argument and a number, num (num < length of arr) as the second argument. The function should construct and return a new array that contains the average of all possible num contiguous numbers of the array. For example − If the input array and the number are − const arr = [1, 2, 3, 4, 5]; const num = 2; Then the output should be − const output = [1.5, 2.5, 3.5, 4.5]; ...

Read More

Checking for uniqueness in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 240 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument. The function should return true if all the numbers in the array appear only once (i.e., all the numbers are unique), and false otherwise. For example − If the input array is − const arr = [12, 45, 6, 34, 12, 57, 79, 4]; Then the output should be − const output = false; because the number 12 appears twice in the array. Method 1: Using indexOf() ...

Read More

Finding the largest non-repeating number in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 446 Views

In JavaScript, finding the largest non-repeating number in an array requires tracking element frequencies and identifying unique values. This problem becomes efficient when we know the value range constraints. We need to write a function that takes an array of integers and returns the largest number that appears exactly once. If no unique number exists, return -1. The constraint given is that all array elements satisfy: 0 < arr[i] < 101 This means all values are between 1 and 100, inclusive. Example Input and Output For the input array: const ...

Read More

Picking all elements whose value is equal to index in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 300 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument. The function should then construct and return a new array based on the original array. The new array should contain all those elements from the original array whose value was equal to the index they were placed on. Note that we have to check the value and index using 1-based index and not the traditional 0-based index. Understanding the Problem For example, if the input array is: const arr = [45, ...

Read More

Checking for permutation of a palindrome in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 706 Views

We are required to write a JavaScript function that takes in a string as the first and the only argument. The task of our function is to check whether any rearrangement in the characters of the string results into a palindrome string or not. If yes, then our function should return true, false otherwise. For a string to form a palindrome through rearrangement, at most one character can have an odd frequency. This is because palindromes read the same forwards and backwards. For example − If the input string is − const str = 'amadm'; ...

Read More

Compressing string in JavaScript

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

We are required to write a JavaScript function that takes in a string that might contain some continuous repeating characters. The function should compress the string like this − 'wwwaabbbb' -> 'w3a2b4' 'kkkkj' -> 'k4j' And if the length of the compressed string is greater than or equal to the original string we should return the original string. For example − 'aab' can be compressed to 'a2b1' but it increases its length to 4 so our function should return 'aab' How It Works The algorithm iterates through each character, counting consecutive ...

Read More

Sum of two elements just less than n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 202 Views

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a single number, num, as the second argument. The function should then find two such numbers whose sum is greatest in the array but just less than the number num. If there exists no two such numbers whose sum is less than num, our function should return -1. Problem Statement Given an array of numbers and a target value, find the maximum sum of any two elements that is still less than the target. For ...

Read More
Showing 2441–2450 of 8,010 articles
« Prev 1 243 244 245 246 247 801 Next »
Advertisements