Front End Technology Articles

Page 357 of 652

Finding elements whose successors and predecessors are in array in JavaScript

AmitDiwan
AmitDiwan
Updated on 27-Feb-2021 506 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 construct and return a new array that contains all such elements from the original array whose successor and predecessor are both present in the array. If means, if any element num is in the original array, it should be included in the result array if and only if num - 1 and num + 1 are also present in the array.For example −If the input array is −const arr = [4, 6, 8, 1, 9, 7, ...

Read More

Checking for majority element in a sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 27-Feb-2021 224 Views

Majority Element:A majority element in an array arr of length l is an element that appears more than l/2 times and hence there is at most one such element.We need to write a JavaScript function, say isMajority() that takes an array arr which is always sorted in the increasing order as the first argument.The second argument of the function will be a number, for which we will search the array and return true if that number is the majority element or false otherwise.For example −If the input array and the number are −const arr = [5, 5, 5, 12, 15]; ...

Read More

Meeting Rooms 2 problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 27-Feb-2021 382 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 take all three meetings due to overlapping times but [10, 20] and [25, 35] can be attended.ExampleThe code ...

Read More

Performing shifts within a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 27-Feb-2021 2K+ Views

Suppose we have a string str containing lowercase English letters, and an array of arrays arr, where arr[i] = [direction, amount] −direction can be 0 (for left shift) or 1 (for right shift).amount is the amount by which string s is to be shifted.A left shift by 1 means remove the first character of s and append it to the end.Similarly, a right shift by 1 means remove the last character of s and add it to the beginning.We are required to write a JavaScript function that takes in the string as the first argument and the array containing shift ...

Read More

Finding the missing number in an arithmetic progression sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 27-Feb-2021 456 Views

Arithmetic Progression:An arithmetic progression (AP) or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant.For instance, the sequence 5, 7, 9, 11, 13...Suppose we have an array that represents elements of arithmetic progression in order. But somehow one of the numbers from the progression goes missing. We are required to write a JavaScript function that takes in one such array as the first and the only argument.Then our function, in one iteration, should find and return the number which is missing from the sequence.For example −If the input array is −const arr ...

Read More

Finding confusing number within an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 27-Feb-2021 295 Views

Confusing Numbers:A number in an array is confusing if it becomes another number which is also present in the array after we rotate the number by 180 degrees vertically and horizontally. For instance, if we rotate 6 by 180 degrees vertically and horizontally it becomes 9 and vice-versa.We have to keep in mind that only rotations of 0, 1, 6, 8, 9 yield a valid number.We are required to write a JavaScript function that takes in a natural number, num, as the first and the only argument. The function should first construct an array of all natural numbers upto num, ...

Read More

Checking digit sum of smallest number in the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 27-Feb-2021 255 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 first pick the smallest number from the array and then calculate the sum of all the digits of the number.If the digit sum of that number is even, we should return true, false otherwise.For example −If the input array is −const arr = [12, 657, 23, 56, 34, 678, 42];Then the output should beconst output = false;because the smallest number in the array is 12 and its digit sum is 1 + 2 = 3, ...

Read More

Finding all valid word squares in JavaScript

AmitDiwan
AmitDiwan
Updated on 27-Feb-2021 292 Views

Word Square:A word square consists of a set of words written out in a square grid, such that the same words can be read both horizontally and vertically.For instance, once valid word square is −H E A R T E M B E R A B U S E R E S I N T R E N DWe are required to write a JavaScript function that takes in an array of words. The function should return true if the array given as input forms a valid word square, false otherwise.For example −If the input word array is −const arr ...

Read More

Find Smallest Letter Greater Than Target in JavaScript

AmitDiwan
AmitDiwan
Updated on 27-Feb-2021 461 Views

Suppose we are given an array of sorted characters letters containing only lowercase letters. And given a target letter target.We are required to write a JavaScript function that takes in the array as the first argument and the letter as the second. The function is supposed to find the smallest element in the list that is larger than the given target.We have to keep in mind that letters also wrap around. For example, if the target is target = 'z' and letters = ['a', 'b'], the answer is 'a'.For Example −If the input array and letter are −const arr = ...

Read More

Counting all possible palindromic subsequence within a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 27-Feb-2021 537 Views

Palindrome Sequence:A string sequence is known as palindrome sequence if it reads the same from front and the back. For instance, 'aba', 'madam, 'did' are all valid palindrome sequences.We are required to write a JavaScript function that takes in a string as the first and the only argument. The string taken as input is guaranteed to consist of only 'a', 'b', 'c' and 'd'. Our function should count and return the number of all contiguous or noncontiguous palindrome subsequences that appear in the string.For example −If the input string is −const str = 'bccb';Then the output should be −const output ...

Read More
Showing 3561–3570 of 6,517 articles
« Prev 1 355 356 357 358 359 652 Next »
Advertisements