Found 6683 Articles for Javascript

Finding the group with largest elements with same digit sum in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:31:10

89 Views

We are required to write a JavaScript function that takes in a positive integer, say n, as the only argument.The function should first group the integers from 1 to n to subarray where a specific subarray contains all the elements contains a specific digit sum. Then the function should examine each subarray and return the length of that subarray which contains the most elements.For example −If the input number is −const num = 15;Then the output should be −const output = 2;because the groups are −[1, 10], [2, 11], [3, 12], [4, 13], [5, 14], [6, 15], [7], [8], [9]ExampleFollowing ... Read More

Grouping words with their anagrams in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:29:21

847 Views

Anagrams:Two words or phrases which can be made by arranging the letters of each other in a different order are called anagrams of each other, like rat and tar.We are required to write a JavaScript function that takes in an array of strings that might contain some anagram strings. The function should group all the anagrams into separate subarrays and return the new array thus formed.For example −If the input array is −const arr = ['rat', 'jar', 'tar', 'raj', 'ram', 'arm', 'mar', 'art'];Then the output array should be −const output = [    ['rat', 'tar', 'art'],    ['jar', 'raj'],   ... Read More

Finding hamming distance in a string in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:27:24

393 Views

Hamming Distance:The hamming distance between two strings of equal length is the number of positions at which these strings vary.In other words, it is a measure of the minimum number of changes required to turn one string into another. Hamming Distance is usually measured for strings equal in length.We are required to write a JavaScript function that takes in two strings, lets say str1 and str2, of the same length. The function should calculate and return the hamming distance between those strings.ExampleFollowing is the code −const str1 = 'Hello World'; const str2 = 'Heeyy World'; const findHammingDistance = (str1 = ... Read More

Counting the number of 1s upto n in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:25:30

213 Views

We are required to write a JavaScript function that takes in a positive integer, say num.The task of our function is to count the total number of 1s that appears in all the positive integers upto n (including n, if it contains any 1).Then the function should finally return this count.For example −If the input number is −const num = 31;Then the output should be −const output = 14;because 1 appears in, 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 31ExampleFollowing is the code −const num = 31; const countOnes = (num = 1) => { ... Read More

Finding the Largest Triple Product Array in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:23:31

127 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument.Based on the array taken in as input, the function should construct a new array of the same length based on the following criteria.Any corresponding element of the output array should be the product of the three largest numbers encountered thus far. If the corresponding index is less than 3 (we have not encountered three elements yet) then the corresponding value should be -1. And Although we can use non-unique values to calculate the product, but those non-unique values should be present ... Read More

Sum of All Possible Odd Length Subarrays in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:21:49

333 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument.The function should first permute all possible subarrays from the original array that have an odd length. And then the function should find the combined sum of all the elements of those subarrays and return the sum.For example −If the input array is −const arr = [1, 2, 3];Then the output should be −const output = 12;because the desired subarrays are [1], [2], [3], [1, 2, 3]ExampleFollowing is the code −const arr1 = [1, 2, 3]; const arr2 = [1, 2, 3, ... Read More

Subarray pairs with equal sums in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:19:52

110 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument.The function should determine whether there exists any way in which we can split the array into two subarray such that the sum of the elements present in the two subarrays are equal. While dividing the elements into subarrays we have to make sure that no element from the original array is left.For example −If the input array is −const arr = [5, 3, 7, 4, 1, 8, 2, 6];Then the output should be −const output = true;because the desired subarrays are: ... Read More

Largest Substring Between Two Equal Characters in a string in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:55:06

154 Views

We are required to write a JavaScript function that takes in a string of characters as the only argument.The function should find that longest string which is sandwiched between two identical characters and return its length.For example −If the input string is −const str = 'sadtrsewak';Then the output should be −const output = 6;because between two 'a' we have the longest desired substring of length 6.ExampleFollowing is the code −const str = 'sadtrsewak'; const longestSubstringBetween = (str = '') => {    const map = {};    let res = -1;    for(let i = 0; i < str.length; i++){ ... Read More

Checking for uniqueness of a string in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:51:19

147 Views

We are required to write a JavaScript function that takes in a string as the first and the only argument. The function should return true if all the characters present in the string are unique. And if even one character appears for more than one, the function should return false.We will use a hash set to keep track of the characters we encounter in the string and if at any stage of iteration, we encounter duplicate characters, we will return false otherwise at the end of iteration, we will return true.ExampleFollowing is the code −const str = 'abschyie'; const checkUniqueness ... Read More

Maximum Product of Two Numbers in a List of Integers in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:48:39

205 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 find the maximum product that can be achieved multiplying any two elements of the array. The condition for us is that we have to do this in linear time and constant space.For example −If the input array is −const arr = [3, 9, 2, 1, 0];Then the output should be −const output = 27;because it’s the greatest product and can be achieved by multiplying 3 and 9.ExampleFollowing is the code −const arr = [3, 9, 2, ... Read More

Advertisements