Object Oriented Programming Articles

Page 32 of 589

Taking common elements from many arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 551 Views

We are required to write a JavaScript function that takes in any arbitrary number of arrays and returns an array of elements that are common to all arrays. If there are no common elements, then we should return an empty array. This problem requires finding the intersection of multiple arrays, which means identifying elements that exist in every provided array. Understanding the Problem The intersection of arrays means finding elements that appear in all arrays. For example, if we have arrays [1, 2, 3], [2, 3, 4], and [3, 4, 5], the common element is 3. ...

Read More

Repeated sum of Number's digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 391 Views

We are required to write a JavaScript function that recursively sums up the digits of a number until it reduces to a single digit number. We are required to do so without converting the number to String or any other data type. How It Works The algorithm uses two functions: sumDigit(): Recursively extracts and sums all digits of a number sumRepeatedly(): Repeatedly calls sumDigit() until result is a single digit Example Let's implement the solution step by step: const num = 546767643; const sumDigit = (num, sum = 0) => ...

Read More

Sorting Array without using sort() in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 8K+ Views

Sorting arrays without using the built-in sort() method is a common programming challenge that helps understand sorting algorithms. JavaScript provides several approaches to implement custom sorting logic. Using Array.reduce() for Insertion Sort The reduce() method can implement insertion sort by building a sorted array incrementally: const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98]; const sortWithReduce = arr => { return arr.reduce((acc, val) => { let ind = 0; while(ind < acc.length ...

Read More

Using one array to help filter the other in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 149 Views

When working with arrays of objects, you often need to filter one array based on values from another array. This is a common requirement when you have a list of items and want to keep only those that match specific criteria. Problem Statement Given an array of objects and an array of values, we need to filter the objects array to include only those objects whose property matches values in the second array. Sample Data Let's start with these arrays: const main = [ {name: "Karan", age: 34}, {name: "Aayush", ...

Read More

Counting smaller and greater in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 403 Views

In JavaScript, we often need to count elements in an array that are greater than or smaller than a specific value. This is useful for data analysis, filtering, and statistical operations. Let's say we have an array of numbers and want to count how many elements are greater than and smaller than a given number n. const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; console.log("Array:", arr); Array: [3, 5, 5, 2, 23, 4, 7, 8, 8, 9] Using reduce() Method The most elegant approach uses the ...

Read More

Is element repeated more than n times in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 222 Views

In JavaScript, you may need to check if any element appears more than a specified number of times in an array. This is useful for data validation, duplicate detection, and enforcing constraints on array contents. Problem Definition We need to write a function that takes two arguments: An Array of literals that may contain repeating elements A number representing the maximum allowed occurrences (limit) The function should return false if any element appears more than the limit, and true otherwise. Using reduce() and every() Methods The ...

Read More

Finding the balance of brackets in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 397 Views

Given a string that consists of only two types of characters: "(" and ")". We are required to write a function that takes in one such string and balances the parentheses by inserting either a "(" or a ")" as many times as necessary. The function should then return the minimum number of insertions made in the string to balance it. For example: If the string is − const str = '()))'; Then the output should be 2, because by prepending '((' we can balance the string. How It Works The algorithm uses ...

Read More

Push specific elements to last in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 194 Views

Suppose we have an array of objects like this: const arr = [ {flag: true, other: 1}, {flag: true, other: 2}, {flag: false, other: 3}, {flag: true, other: 4}, {flag: true, other: 5}, {flag: true, other: 6}, {flag: false, other: 7} ]; We are required to write a JavaScript function that takes in one such array and sorts it based on the following conditions: If arr.flag === false, the matching ...

Read More

Greatest number in a dynamically typed array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 178 Views

We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some false values. Our function should return the biggest Number from the array. For example: If the input array is − const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; Then the output should be 65. Using Math.max with Array Filtering The most straightforward approach is to filter valid numbers first, then use Math.max(): const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; ...

Read More

How to get all combinations of some arrays in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 361 Views

Getting all combinations of arrays in JavaScript can be accomplished using recursive generator functions or iterative approaches. This is useful for creating permutations, product combinations, and other algorithmic solutions. Using Generator Function (Cartesian Product) A generator function can efficiently create all combinations by recursively building each possibility: function getAllCombinations(values) { function* generateCombinations(size, current) { if (size) { for (var element of values) { ...

Read More
Showing 311–320 of 5,881 articles
« Prev 1 30 31 32 33 34 589 Next »
Advertisements