Found 6710 Articles for Javascript

How do I recursively remove consecutive duplicate elements from an array?

AmitDiwan
Updated on 25-Aug-2020 07:15:11

627 Views

Suppose, we have an array of Number literals that contains some consecutive redundant entries like this −const testArr = [1, 1, 2, 2, 3, 3, 1, 1, 1];We are supposed to write a function compress that takes in this array and removes all redundant consecutive entries in place. So that the output looks like this −const output = [1, 2, 3, 1];Let’s write the code for this function, we will be using recursion for this and the code for this will be −Exampleconst testArr = [1, 1, 2, 2, 3, 3, 1, 1, 1]; const compress = (arr, len = ... Read More

Check if some elements of array are equal JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:13:13

398 Views

We have an array of numbers that have got some redundant entries, our job is to write a function that takes in the array and groups all the identical entries into one subarray and returns the new array thus formed.For example −//If the input array is: const arr = [1, 3, 3, 1]; //then the output should be: const output = [[1, 1], [3, 3]];We will use a HashMap to keep a track of the elements already occurred and iterate over the array using a for loop, the code for this will be −Exampleconst arr = [1, 3, 3, 1]; ... Read More

How to find distance between items on array JavaScript?

Nikitasha Shrivastava
Updated on 18-May-2023 14:08:45

1K+ Views

In this problem statement, our aim is to find the distance between items on an array with the help of Javascript. So for doing this task we will use a loop and a variable to store the id for each object in the JSON object. Understanding the problem statement The problem statement is to write a function in Javascript by which we can find the distance between items on an array. If we have an array, the distance will be measured by counting the index numbers from the first item from where we have to measure the distance. For example ... Read More

Flatten an array in JavaScript.

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

310 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 JavaScript

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

260 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

Counting how many times an item appears in a multidimensional array in JavaScript

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

998 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

How can I split an array of Numbers to individual digits in JavaScript?

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

485 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 JavaScript

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

649 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

How to find a group of three elements in an array whose sum equals some target sum JavaScript

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

570 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 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

Advertisements