Object Oriented Programming Articles

Page 149 of 589

JavaScript Determine the array having majority element and return TRUE if its in the same array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 207 Views

We are required to write a JavaScript function that takes in an array of numbers with repetitive values and returns the number that appears for more than (n/2) number of times where n is the length of the array. If there is no such element in the array, our function should return false. This problem is commonly known as finding the "majority element" in an array. A majority element is one that appears more than half the time in the array. Algorithm Approach We'll use the Boyer-Moore Voting Algorithm, which works in two phases: ...

Read More

Comparing forEach() and reduce() for summing an array of numbers in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 498 Views

When summing arrays in JavaScript, both forEach() and reduce() are common approaches. This article compares their performance to help you choose the right method. Since we can't demonstrate with truly massive arrays here, we'll simulate the performance impact by running the summing operation many times in a loop. The Two Approaches Here's how each method works for summing an array: const arr = [1, 4, 4, 54, 56, 54, 2, 23, 6, 54, 65, 65]; // Using reduce() - functional approach const reduceSum = arr => arr.reduce((acc, val) => acc + val); // ...

Read More

Return TRUE if the first string starts with a specific second string JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 258 Views

We are required to write a JavaScript function that takes in two strings and checks whether the first string starts with the second string or not. For example: If the two strings are: "Disaster management report" "Disas" Then our function should return true There are multiple ways to check if a string starts with another string in JavaScript. Let's explore the most common approaches. Using startsWith() Method (Recommended) The startsWith() method is the built-in and most straightforward way to check if a string starts with another string: const first = 'The ...

Read More

Summing numbers from a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 996 Views

We are required to write a JavaScript function that takes in a string that contains some one-digit numbers in between and the function should return the sum of all the numbers present in the string. Let's say the following is our string with numbers: const str = 'gdf5jhhj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; Method 1: Using for Loop with Type Conversion This approach splits the string into characters and checks each one using type conversion: const str = 'gdf5jhhj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; const sumStringNum = str => { const strArr = str.split(""); let ...

Read More

Check for Subarray in the original array with 0 sum JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 503 Views

We are required to write a JavaScript function that takes in an array of Numbers with some positive and negative values. We are required to determine whether there exists a subarray in the original array whose net sum is 0 or not. Our function should return a boolean on this basis. Approach The approach here is simple. We iterate over the array using a for loop, calculate the cumulative sum up to that particular element. And if any point the cumulative becomes 0 or attains a value it has previously attained, then there exists a subarray with ...

Read More

Finding difference of greatest and the smallest digit in a number - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 439 Views

We are required to write a JavaScript function that takes in a number and returns the difference between the greatest and the smallest digit present in it. For example: If the number is 5464676, then the smallest digit here is 4 and the greatest is 7 Hence, our output should be 3 Example Let's write the code for this function — const num = 44353456; const difference = (num, min = Infinity, max = -Infinity) => { if(num){ const digit = num ...

Read More

Split string into equal parts JavaScript

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

In JavaScript, splitting a string into equal parts can be accomplished in several ways. This article demonstrates how to split a string into n equal parts using an alternating pattern that takes characters from both ends of the string. Problem Statement We need to write a JavaScript function that takes a string and a number n as arguments, where n exactly divides the string length. The function should return an array of n strings of equal length, formed by alternating between the first and last characters of the remaining string. For example: If the string ...

Read More

Object to array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 373 Views

Converting an object to an array of key-value pairs is a common task in JavaScript. There are several built-in methods to achieve this transformation. Sample Object Let's start with this example object: const obj = { name: "Vikas", age: 45, occupation: "Frontend Developer", address: "Tilak Nagar, New Delhi", experience: 23, salary: "98000" }; Using Object.entries() (Recommended) The simplest approach is using Object.entries(), which directly converts an object to an ...

Read More

JavaScript R- eturn Array Item(s) With Largest Score

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

We have an array of arrays that contains the marks scored by some students in different subjects. We need to write a function that returns the top scorer(s) for each subject, handling cases where multiple students have the same highest score. Problem Setup Given this input data: const arr = [ ['Math', 'John', 100], ['Math', 'Jake', 89], ['Math', 'Amy', 93], ['Science', 'Jake', 89], ['Science', 'John', 89], ['Science', 'Amy', 83], ...

Read More

Split number into n length array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 412 Views

We are required to write a JavaScript function that takes in two numbers say m and n, and returns an array of size n with all the elements of the resulting array adding up to m. Let's write the code for this function − Example Following is the code − const len = 8; const sum = 5; const splitNumber = (len, sum) => { const res = []; for(let i = 0; i < len; i++){ res.push(sum / ...

Read More
Showing 1481–1490 of 5,881 articles
« Prev 1 147 148 149 150 151 589 Next »
Advertisements