AmitDiwan has Published 10744 Articles

Filter unique array values and sum in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 09:30:24

698 Views

Suppose, we have an array of arrays like this −const arr = [[12345, "product", "10"], [12345, "product", "15"], [1234567, "other", "10"]];We are supposed to write a function that takes in one such array. Notice that all the subarrays have precisely three elements in them.Our function should filter out that subarray ... Read More

The algorithm problem - Backtracing pattern in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 09:27:55

217 Views

Consider the following backtracing problem: On a 2−dimensional grid, there are 4 types of squares −1 represents the starting square. There is exactly one starting square.2 represents the ending square. There is exactly one ending square.0 represents empty squares we can walk over.−1 represents obstacles that we cannot walk over.We ... Read More

Take an array of integers and create an array of all the possible permutations in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 09:24:10

327 Views

We are required to write a function that does the following −takes an array of integers as an argument (e.g. [1, 2, 3, 4])creates an array of all the possible permutations of [1, 2, 3, 4], with each permutation having a length of 4 (i.e., the length of original array)ExampleThe ... Read More

Creating an array of objects based on another array of objects JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:50:37

663 Views

Suppose, we have an array of objects containing data about likes of some users like this −const arr = [    {"user":"dan", "liked":"yes", "age":"22"},    {"user":"sarah", "liked":"no", "age":"21"},    {"user":"john", "liked":"yes", "age":"23"},  ];We are required to write a JavaScript function that takes in one such array. The function should construct ... Read More

Creating permutations to reach a target number, but reuse the provided numbers JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:49:10

172 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a target sum Number as the second argument.The function should return an array of all those subarrays from the original array whose elements sum to make the target sum. We ... Read More

Transform tree from DB format to JSON format in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:47:22

455 Views

Suppose, we have an array of objects like this −const arr = [    {"id":7, "name":"Kuwait", "parentId":2},    {"id":4, "name":"Iraq", "parentId":2},     {"id":10, "name":"Qatar", "parentId":2},    {"id":2, "name":"Middle East", "parentId":1},    {"id":3, "name":"Bahrain", "parentId":2},    {"id":6, "name":"Jordan", "parentId":2},    {"id":8, "name":"Lebanon", "parentId":2},    {"id":1, "name":"Africa/Middle East", "parentId":null},     ... Read More

Relative sorting in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:42:38

337 Views

Suppose, we have two arrays, let’s say arr1 and arr2. The elements of arr2 are distinct, and all elements in arr2 are also in arr1.We are required to write a JavaScript function that takes in two such arrays and sorts the elements of arr1 such that the relative ordering of ... Read More

Longest subarray which only contains strictly increasing numbers JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:41:24

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 return the length of the longest continuous subarray from the array that only contains elements in a strictly increasing order.A strictly increasing sequence is the ... Read More

Finding degree of subarray in an array JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:40:03

560 Views

The degree of an array of literals is defined as the maximum frequency of any one of its elements.const arr = [1, 2, 3, 3, 5, 6, 4, 3, 8, 3];The degree of this array is 4, because 3 is repeated 4 times in this array.We are required to write ... Read More

Complicated array grouping JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:36:36

190 Views

Suppose we have an array of objects like this −const arr = [    {userId: "3t5bsFB4PJmA3oTnm", from: 1, to: 6},    {userId: "3t5bsFB4PJmA3oTnm", from: 7, to: 15},    {userId: "3t5bsFB4PJmA3oTnm", from: 172, to: 181},    {userId: "3t5bsFB4PJmA3oTnm", from: 182, to: 190} ];We are required to write a JavaScript function that ... Read More

Advertisements