Flatten an Array in JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:08:53

326 Views

We are required to write a JavaScript array function that takes in a nested array and returns an array with all the elements present in the array without any nesting.For example −//if the input is: const arr = [[1, 2, 3], [4, 5], [6]]; //then the output should be: const output = [1, 2, 3, 4, 5, 6];Therefore, let’s write the code for this function −Method 1: Using recursionHere we will loop over the original nested array and recursively push the nested element elements into a new array.Exampleconst arr = [[1, 2, 3], [4, 5], [6]]; const flatten = function(){ ... Read More

Get Minimum Number Without a Math Function in JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:06:36

269 Views

We have to write a function that takes in n Number literals as argument, where n is any whole number and returns the smallest number of those numbers without using any library function.We will solve this problem via a while loop and the code for this will be −Exampleconst numbers = [12, 5, 7, 43, -32, -323, 5, 6, 7, 767, 23, 7]; const findMin = (...numbers) => {    let min = Infinity, len = 0;    while(len++ < numbers.length){       min = numbers[len] < min ? numbers[len] : min;    }    return min; }; console.log(findMin(...numbers));OutputThe output in the console will be −-323

Count Item Occurrences in a Multidimensional Array in JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:05:36

1K+ Views

We have a nested array of strings and we have to write a function that accepts the array and a search string and returns the count of the number of times that string appears in the nested array.Therefore, let’s write the code for this, we will use recursion here to search inside of the nested array and the code for this will be −Exampleconst arr = [    "apple",    ["banana", "strawberry", "dsffsd", "apple"],    "banana",    ["sdfdsf", "apple", ["apple", ["nonapple", "apple", ["apple"]]]]    , "apple"];    const calculateCount = (arr, query) => {       let count = ... Read More

Split an Array of Numbers to Individual Digits in JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:03:37

496 Views

We have an array of Number literals, and we are required to write a function, say splitDigit() that takes in this array and returns an array of Numbers where the numbers greater than 10 are splitted into single digits.For example −//if the input is: const arr = [ 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106 ] //then the output should be: const output = [ 9, 4, 9, 5, 9, 6, 9, 7, 9, 8, 9, 9, 1, 0, 0, 1, 0, 1, 1, 0, 2, 1, 0, 3, 1, 0, 4, 1, 0, ... Read More

Intersection of Two Arrays in JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:01:52

665 Views

We have two arrays of Numbers, and we are required to write a function, say intersection() that computes their intersection and returns an array that contains the intersecting elements in any order. Each element in the result should appear as many times as it shows in both arrays.For example − If, Input: arr1 = [1, 2, 3, 1], arr2 = [1, 3, 1] Output: [1, 3, 1]ApproachHad the arrays been sorted, we could have used the two pointer approach with initially both pointing to 0 the start of the respective array and we could have proceeded with increasing the corresponding ... Read More

Find Three Elements in an Array Whose Sum Equals Target in JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:00:29

582 Views

We have to write a function, say threeSum() that takes in an array of Numbers and a target sum. It checks whether there exist any three numbers in the array that add up to the target sum, if there exist such three numbers in the array, then it should return their indices in an array otherwise it should return -1.ApproachThe approach is simple, We will first write a function twoSum(), that takes in an array and a target sum and takes linear time and space to return the indices of two numbers that add up to target sum otherwise -1.Then ... Read More

Convert Array of Arrays to Array of Objects Grouped Together in JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:59:09

1K+ Views

Let’s say, we have a two-dimensional array that contains data about some colors and fruits like thisconst data = [    ['orange', 'fruit'],    ['red', 'color'],    ['green', 'color'],    ['orange', 'color'],    ['banana', 'fruit'],    ['blue', 'color'],    ['lemon', 'fruit'],    ['mango', 'fruit'],    ['lemon', 'color'], ];We have to write a function that takes in this array and returns an array in which the different fruits and colors are grouped by their categories.Like in this example we only have two categories namely ‘fruit’ and ‘color’, so we should expect an array of two objects in the output like this ... Read More

Convert 2D Array to Object Using Map or Reduce in JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:56:41

1K+ Views

Let’s say, we have a two-dimensional array that contains some data about the age of some people.The data is given by the following 2D arrayconst data = [    ['Rahul', 23],    ['Vikky', 27],    ['Sanjay', 29],    ['Jay', 19],    ['Dinesh', 21],    ['Sandeep', 45],    ['Umesh', 32],    ['Rohit', 28], ];We are required to write a function that takes in this 2-D array of data and returns an object with key as the first element of each subarray i.e., the string and value as the second element.We will use the Array.prototype.reduce() method to construct this object, and the ... Read More

Multiply Odd Index Values in JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:52:37

389 Views

We are required to write a function, that takes in an array of Number literals as one and the only argument. The numbers that are situated at even index should be returned as it is. But the numbers situated at the odd index should be returned multiplied by their corresponding indices.For example −If the input is:    [5, 10, 15, 20, 25, 30, 50, 100] Then the function should return:    [5, 10, 15, 60, 25, 150, 50, 700]We will use the Array.prototype.reduce() method to construct the required array and the code for the function will be −Exampleconst arr = ... Read More

Find Character Combination in Array of Strings using JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:51:37

141 Views

We have to write a function that accepts an array of strings and a string. Our job is to check whether the array contains any sequence or subsequence of the string as its element or not, and the function should return a boolean based on this fact.For instance −const x = 'ACBC'; const arr = ['cat', 'AB']; const arr2 = ['cat', '234', 'C']; const arr3 = ['cat', 'CC']; const arr4 = ['cat', 'BB']; console.log(containsString(arr, x)) // true console.log(containsString(arr2, x)) // true console.log(containsString(arr3, x)) // true console.log(containsString(arr4, x)) // falseTherefore, let’s write the code for this function −Exampleconst x = 'ACBC'; ... Read More

Advertisements