Javascript Articles

Page 147 of 534

Calculating average of a sliding window in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 685 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 219 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 441 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 283 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 698 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 189 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

Checking for majority element in a sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 232 Views

A majority element in an array of length l is an element that appears more than l/2 times. In a sorted array, if a majority element exists, it must occupy the middle position since it spans over more than half the array. Problem Definition We need to write a JavaScript function isMajority() that takes a sorted array and a number, returning true if that number is the majority element, false otherwise. Key Insight for Sorted Arrays In a sorted array, if there exists a majority element, it will always be the middle element. This is because ...

Read More

Meeting Rooms 2 problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 393 Views

We will be given an array of arrays, each subarray consists of exactly two elements indicating the start and end time of a meeting. The task of our function is to find the maximum number of meetings one person can take avoiding the conflict of time. The function should finally return this number. For example − If the input array describing meeting times is − const arr = [[5, 40], [10, 20], [25, 35]]; Then the output should be − const output = 2; because it's not possible to ...

Read More

Performing shifts within a string in JavaScript

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

In JavaScript, string shifting involves moving characters from one end of a string to the other. This is commonly used in algorithms and data manipulation tasks where you need to rotate string characters based on specific directions and amounts. String shifts can be performed in two directions: Left shift (0): Remove characters from the beginning and append them to the end Right shift (1): Remove characters from the end and prepend them to the beginning Understanding Shift Operations Let's see how individual shifts work: // Left shift by 1: "abc" -> "bca" ...

Read More
Showing 1461–1470 of 5,340 articles
« Prev 1 145 146 147 148 149 534 Next »
Advertisements