Found 9313 Articles for Object Oriented Programming

Intersection of two arrays JavaScript

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

546 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

478 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

840 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

How to multiply odd index values JavaScript

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

282 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 char combination in array of strings JavaScript

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

90 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

Calculating least common of a range JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:49:34

64 Views

We are required to write a function that takes in an array of two numbers a and b (a >= b) and returns the least common multiple of all the numbers between [a, b].ApproachWe will first write a basic function that calculates the least common multiple of two numbers, once we have that we will recursively call it over the numbers that fall between [a, b] and finally return the result.Exampleconst lcm = (a, b) => {    let min = Math.min(a, b);    while(min >= 2){       if(a % min === 0 && b % min === ... Read More

Write a program to calculate the least common multiple of two numbers JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:46:45

640 Views

We are required to write a function that accepts two numbers and returns their least common multiple.Least Common Multiple (LCM)The least common multiple of two numbers a and b is the smallest positive integer that is divisible by both a and b.For example − The LCM of 6 and 8 is 24 because 24 is the smallest positive integer that is divided by both 6 and 8.Method to calculate LCMOne of the many ways of calculating LCM of two numbers a and b is by dividing the product of a and b by the greatest integer (also known as greatest ... Read More

map() array of object titles into a new array based on other property value JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:44:54

165 Views

Let’s say, we have an array of objects like this −const arr = [{    country: "cananda",    count: 2 }, {       country: "jamaica",       count: 2 }, {       country: "russia",       count: 1 }, {       country: "india",       count: 3 }, {       country: "spain",       count: 2 }, {       country: "portugal",       count: 1 }, {       country: "italy",       count: 1 }];We are required to write a function that ... Read More

JavaScript equivalent of Python's zip function

AmitDiwan
Updated on 25-Aug-2020 06:42:45

957 Views

We have to write the JavaScript equivalent function of Python's zip function. That is, given multiple arrays of equal lengths, we are required to create an array of pairs.For instance, if I have three arrays that look like this −const array1 = [1, 2, 3]; const array2 = ['a', 'b', 'c']; const array3 = [4, 5, 6];The output array should be −const output = [[1, 'a', 4], [2, 'b', 5], [3, 'c', 6]]Therefore, let’s write the code for this function zip(). We can do this in many ways like using the reduce() method or the map() method or by using ... Read More

Advertisements