Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 245 of 801
Finding minimum steps to make array elements equal in JavaScript
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 MoreFinding even length numbers from an array in JavaScript
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 MoreFinding average of n top marks of each student in JavaScript
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 MoreCalculating average of a sliding window in JavaScript
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 MoreChecking for uniqueness in an array in JavaScript
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 MoreFinding the largest non-repeating number in an array in JavaScript
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 MorePicking all elements whose value is equal to index in JavaScript
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 MoreChecking for permutation of a palindrome in JavaScript
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 MoreCompressing string in JavaScript
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 MoreSum of two elements just less than n in JavaScript
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