Found 9313 Articles for Object Oriented Programming

Smallest positive value that cannot be represented as sum of subarray JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:52:36

82 Views

We have a sorted array of positive integers like this −const arr = [1, 3, 6, 10, 11, 15];We are required to write a function, say findSmallest() that takes in one such array and returns the smallest positive integer that cannot be represented as the sum of some subarray of this original array.For example −For this array written above 2 is the smallest positive integer which cannot be reached by summing any subarray of this original array. So, now let's write the code for this function. As the array is sorted, we can achieve the solution to this problem in ... Read More

Return Top two elements from array JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:51:08

243 Views

We have an array of numbers in JavaScript that contains numbers in an unsorted order. Our job is to write a function that takes in this array of numbers and returns an array of two elements, the top two elements of the array (greatest two elements of the array).We have to do this in one pass i.e., we need to execute this method in linear time like by using only one for loop or if we use ES6 function, we have to make sure to use only one and once and avoid nesting of methods which increases time complexity.So, now ... Read More

Finding first non-repeating character JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:49:38

639 Views

We have an array of Numbers/String literals where most of the entries are repeated. Our job is to write a function that takes in this array and returns the index of first such element which does not make consecutive appearances.If there are no such elements in the array, our function should return -1. So now, let's write the code for this function. We will use a simple loop to iterate over the array and return where we find non-repeating characters, if we find no such characters, we return -1 −Exampleconst arr = ['d', 'd', 'e', 'e', 'e', 'k', 'j', 'j', ... Read More

Find the difference of largest and the smallest number in an array without sorting it in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:45:54

242 Views

We have an array of Numbers that are arranged in pure random order. Our job is to write a function that takes in one such array of Numbers and returns the difference of greatest and smallest numbers present in it, but without sorting the array.Therefore, let's write the code for this function −We will use the Array.prototype.reduce() function to pick the smallest and greatest numbers from the array and later will return their difference. The code for this function will be −Exampleconst arr = [23, 65, 67, 23, 2, 6, 87, 23, 45, 65, 3, 234, 3]; const findDifference = ... Read More

JavaScript array: Find all elements that appear more than n times

AmitDiwan
Updated on 31-Aug-2020 06:44:32

308 Views

We have an array of Number/String literals that contains some repeated entries. Our job is to write a function that takes in a positive integer Number n and returns a subarray of all the elements that makes appearance more than or equal to the number n specified by the only argument.Therefore, let's write the code for this function −We will use a Map() to keep count of the frequency of elements and later return the elements that exceed the specified count. The code for this will be −Exampleconst arr = [34, 6, 34, 8, 54, 7, 87, 23, 34, 6, ... Read More

Searching objects by value in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:43:17

85 Views

Suppose we have an object like this −const obj = {    "name": "Vivek Sharma",    "occupation": "Software Engineer",    "age": 23,    "contacts": [{       "name": "Mukul Sharma",       "occupation": "Software Engineer",       "age": 31,    }, {       "name": "Jay Sharma",       "occupation": "Software Engineer",       "age": 27,    }, {       "name": "Rajan Sharma",       "occupation": "Software Engineer",       "age": 32,    }] };Here it is nested up to only one level, but the nesting can be deeper as ... Read More

Reverse index value sum of array in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:41:10

164 Views

Suppose we have an array of numbers like this −const arr = [3, 6, 7, 3, 1, 4, 4, 3, 6, 7];This array in the example contains 10 elements, so the index of last element happens to be 9. We are required to write a function that takes in one such array and returns the reverse index multiplied sum of the elements.Like in this example, it would be something like −(9*3)+(8*6)+(7*7)+(6*3)+.... until the end of the array.Therefore, let's write the code for this function −Exampleconst arr = [3, 6, 7, 3, 1, 4, 4, 3, 6, 7]; const reverseMultiSum = ... Read More

Solution to the clumsy factorial problem in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:40:11

304 Views

Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order.For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * ... Read More

Return the difference between the maximum & minimum number formed out of the number n in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:37:24

275 Views

We have to write a function maximumDifference() that takes in a positive number n and returns the difference between the maximum number and the minimum number that can be formed out of the number n.For example −If the number n is 203, The maximum number that can be formed from its digits will be 320The minimum number that can be formed from its digits will be 23 (placing the zero at one’s place)And the difference will be −320-23 = 297Therefore, the output should be 297Let's write the code for this function −Exampleconst digitDifference = num => {    const asc ... Read More

Vowel, other characters and consonant difference in a string JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:35:50

146 Views

We are required to write a function that takes in a string of definite characters and the function should return the difference between the count of vowels plus other characters and consonants in the string.For example −If the string is −"HEllo World!!"Then, we have 7 consonants, 3 vowels and 3 other characters here so the output should be −|7 - (3+3)| = 1Hence, the output should be 1Let's write the code for this function −Exampleconst str = 'HEllo World!!'; const findDifference = str => {    const creds = str.split("").reduce((acc, val) => {       let { v, c ... Read More

Advertisements