Found 6683 Articles for Javascript

Finding intersection of arrays that contain repetitive entries in JavaScript

AmitDiwan
Updated on 18-Jan-2021 05:06:49

85 Views

We are required to write a JavaScript function that takes in two arrays of numbers, lets call them arr1 and arr2.The function should build a third array based on the two input arrays that contains all the elements that are common to both arr1 and arr2.Note that if there are more than one same element that are present in both the arrays then we have to consider all such instances of that element.For example −If the input arrays are −const arr1 = [1, 2, 2, 4, 4, 5, 6]; const arr2 = [3, 2, 4, 2, 4, 9];Then the output ... Read More

Alternative sorting of an array in JavaScript

AmitDiwan
Updated on 18-Jan-2021 05:04:45

507 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first and the only argument.The job of our function is to sort the elements present in the array in an alternate fashion.By alternate we mean the following −Suppose we have an array arr that contains only four elements for the time being. Then our function should shuffle the elements of the array such that −arr[0] < arr[1] > arr[2] < arr[3]Note that there can be more than one possible solution for a given array, we are just required to return any of the ... Read More

Calculating h index of a citation in JavaScript

AmitDiwan
Updated on 18-Jan-2021 04:59:58

282 Views

Suppose we have an array of positive integers that represents the number of citations a particular researcher has conducted over a period of time.We are required to write a JavaScript function that takes in one such array and the function should find the h-index of that researcher based on the citations data represented by the array.H-Index:Consider a researcher who performed N number of citations in his career. Then the researcher has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each.For example ... Read More

Finding special kind of elements with in an array in JavaScript

AmitDiwan
Updated on 18-Jan-2021 04:57:59

62 Views

We are required to write a JavaScript function that takes in three arguments, namely −arr --> an array of integers m --> a positive integer n --> a positive integerThe task of our function is to find out whether there exists two such elements (lets call them a1 and a2) such that −The absolute difference between a1 and a2 is at most mThe absolute difference between the indices of a1 and a2 is at most nExampleFollowing is the code −const arr = [1, 2, 3, 1, 7, 8]; const findSpecialElements = (arr = [], m, n) => {    const ... Read More

Constructing largest number from an array in JavaScript

AmitDiwan
Updated on 18-Jan-2021 04:55:35

444 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 string together the numbers present in the array such that form the greatest possible number that can be formed from those given set of numbers.For example −If the input array is −const arr = [5, 45, 34, 9, 3];Then the output should be −const output = '9545343';ExampleFollowing is the code −const arr = [5, 45, 34, 9, 3]; const largestNumber = (arr = []) => {    if(arr.every( n => n === 0)){       ... Read More

Inserting a new interval in a sorted array of intervals in JavaScript

AmitDiwan
Updated on 18-Jan-2021 04:53:57

469 Views

For the purpose of this question, we define an interval as an array of two numbers where the first number is always smaller than the second number.For example −[4, 6], [2, 3], [6, 8], [2, 7], [1, 8] are all examples of valid intervals.Suppose, we have an array of intervals which is sorted according to the their start times (the first elements of each interval).The intervals in the array are non-overlapping which means that for any two arbitrary adjacent intervals, [m, n], [x, y] m < n < x < yTherefore, one such example of this array of intervals can ... Read More

Sorting array of exactly three unique repeating elements in JavaScript

AmitDiwan
Updated on 18-Jan-2021 04:50:46

203 Views

Suppose we have an array of Numbers that contains any frequency of exactly three elements - 1, 0 and 1 like this −const arr = [1, 1, 0, -1, 1, 0, -1, 1, 0, 0, 1];We are required to write a JavaScript function that takes in one such array. The function should simply sort this special array in place i.e., without using any extra array to store the values.The only condition is that our function should be a linear time function (using only one iteration).ExampleFollowing is the code −const arr = [1, 1, 0, -1, 1, 0, -1, 1, 0, ... Read More

Total possible ways of making sum of odd even indices elements equal in array in JavaScript

AmitDiwan
Updated on 18-Jan-2021 04:48:47

207 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 try removing one such element from the array, upon removal of which, the sum of elements at odd indices is equal to the sum of elements at even indices. In the way, the function should count all the possible unique ways in which we can remove one element at a time to achieve the required combination.For example −If the input array is −const arr = [2, 6, 4, 2];Then the output should be 2 because, ... Read More

Convert an array of objects into plain object in JavaScript

AmitDiwan
Updated on 18-Jan-2021 04:45:35

508 Views

Suppose we have an array of objects like this −const arr = [{    name: 'Dinesh Lamba',    age: 23,    occupation: 'Web Developer', }, {    address: 'Vasant Vihar',    experience: 5,    isEmployed: true }];We are required to write a JavaScript function that takes in one such array of objects. The function should then prepare an object that contains all the properties that exist in all the objects of the array.Therefore, for the above array, the output should look like −const output = {    name: 'Dinesh Lamba',    age: 23,    occupation: 'Web Developer',    address: 'Vasant ... Read More

Finding n subarrays with equal sum in JavaScript

AmitDiwan
Updated on 18-Jan-2021 04:43:45

89 Views

We are required to write a JavaScript function that takes in an array of integers as the first argument and an integer as the second argument.The function should check whether we can create n (second argument) subarrays from the original array such that all the subarrays have the equal sum.For example −If the inputs are −const arr = [4, 3, 2, 3, 5, 2, 1]; const num = 4;The output should be true because the subarrays are: [5], [1, 4], [2, 3], [2, 3] all having sum equal to 5.ExampleFollowing is the code −const arr = [4, 3, 2, 3, ... Read More

Advertisements