Found 9150 Articles for Object Oriented Programming

Group array by equal values JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:43:26

1K+ Views

Let’s say, we have an array of string / number literals that contains some duplicate values like this −const array = ['day', 'night', 'afternoon', 'night', 'noon', 'night', 'noon', 'day', 'afternoon', 'day', 'night'];We are required to write a function groupSimilar() that takes in this array and returns a new array where all the repeating entries are group together in a subarray as the first element and their total count in the original array as the second element.So, for this example, the output should be −[    [ 'day', 3 ],    [ 'night', 4 ],    [ 'afternoon', 2 ],   ... Read More

Changing an array in place using splice() JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:41:48

184 Views

We are required to write a function that, given an array arr and a number n, returns the array with elements repeating no more than n times. And we have to do all this without disturbing the indices of desired elements. So, let’s write the code for this function, We will keep the count of all the elements in a hashmap and during iteration whenever the count of any element exceeds the maximum count we will splice that element. The code for this will be −Exampleconst arr = [7, 26, 21, 41, 43, 2, 26, 24, 10, 26, 10, 10, ... Read More

Find the middle element of an array using recursion JavaScript

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

460 Views

We are required to write an array function, say findMiddle that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops. If the array contains an odd number of elements, we return the one, middlemost element, or if the array contains an even number of elements, we return an array of two middlemost elements.So, let’s write the code for this function. As you’ve already guessed, we will be making use of recursion to find these elements. The code for the recursive function will be −Exampleconst arr = [1, 2, 3, ... Read More

Check whether a series of operations yields a given number with JavaScript Recursion

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

122 Views

By starting from the number 1 and repeatedly either adding 5 or multiplying by 3, an infinite amount of new numbers can be produced. We are required to write a function that, given a number, tries to find a sequence of such additions and multiplications that produce that number. And returns a boolean based on the fact whether or not there exists any such sequenceFor example, The number 13 could be reached by first multiplying by 3 and then adding 5 twice, so the function should return true for 13. Whereas the number 15 cannot be reached at all, so ... Read More

Chunking an array in JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:32:50

769 Views

We are required to write a function chunk() that takes in an array arr of string / number literals as the first argument and a number n as second argument.We are required to return an array of n subarrays, each of which contains at most arr.length / n elements. And the distribution of elements should be like this −The first element goes in the first subarray, second in second, third in third and so on. Once we have one element in each subarray, we again start with filling the first subarray with its second element. Similarly, when all subarrays have ... Read More

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

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

624 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

396 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

309 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

258 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

Advertisements