Category: Array

Implement reduce in javascript

Reducing an array to a single value Often we have to iterate over the array by accessing multiple items at a time. For example, if you have to find the largest element in an...

Implement concatAll in javascript

Querying Nested Arrays To work with tree data structure we need to flatten them. We will be solving a problem using Array.prototype.forEach(). We will define Array.prototype.concatAll() using Array.prototype.forEach(). We will solve the same problem...

Lambda operator filter and map chaining

Problem Filter only those videos with a rating of 5.0 and return the id of such videos. Solution Further Reading Functional Programming

Implement filter in javascript

Predicating Arrays Filtering also key method for manipulating collections. We iterate over the array and add an item to the new array if it passes the test. We will be solving a problem using...

Implement map in javascript

Projecting Arrays Applying a value to a function and getting a new value is called a projection. To project (transform) one array to another, we have to process each element in the array and...

Use map to accept an additional argument in javascript

Problem Write a function Array.prototype.map() which can accept one additional argument. function mul(x) { return x * x; } console.log([1, 2, 3].map(mul)); // [1, 4, 9] Modify the function so that it can accept...

find pivot in an sorted rotated array

Problem : Given a sorted and rotated array find the index of a pivot element in an array. Input : An array Output : An index Algorithm : Binary search tree Time complexity :...

Dedupe an array in javascript

Input : An array Output : An array Logic : Iterate over the input array called data Add the element to the output array called result if the element does not exist in the...

Find duplicates in an array using javascript

Problem : Given an array of positive integers find all the duplicate elements. Algorithm : Iterate over the array using forEach Find if there is a duplicate for the element using indexOf indexOf takes...

Create a bar chart from json

Problem : Given a JSON data structure with key representing age and value representing the number of people with that age. For example, { 25: 55, 26: 45, 27: 10, 28: 20, 30: 1,...