Object Oriented Programming Articles

Page 15 of 589

Finding sum of a range in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 367 Views

We are required to write an Array function (functions that lives on Array.prototype object). The function should take in a start index and an end index and it should sum all the elements from start index to end index in the array (including both start and end). Example const arr = [1, 2, 3, 4, 5, 6, 7]; const sumRange = function(start = 0, end = 1){ let sum = 0; if(start > end){ return sum; } for(let i = start; i

Read More

Finding the majority element of an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 885 Views

We are given an array of size n, and we are required to find the majority element. The majority element is the element that appears more than [ n/2 ] times. Example const arr = [2, 4, 2, 2, 2, 4, 6, 2, 5, 2]; const majorityElement = (arr = []) => { const threshold = Math.floor(arr.length / 2); const map = {}; for (let i = 0; i < arr.length; i++) { const value = arr[i]; ...

Read More

Find the least duplicate items in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 470 Views

We are required to write a JavaScript function that takes in an array of literals which may contain some duplicate values. The function should return an array of all those elements that are repeated for the least number of times. For example− If the input array is − const arr = [1, 1, 2, 2, 3, 3, 3]; Then the output should be − const output = [1, 2]; because 1 and 2 are repeated for the least number of times (2) Example const arr = [1, ...

Read More

Check if a value exists in an array and get the next value JavaScript

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

We are required to write a JavaScript function that takes in an array of literals as the first argument and a search string as the second argument. The function should search the array for that search string. If that string exists in the array, we should return its next element from the array, otherwise we should return false. Example const arr = ["", "comp", "myval", "view", "1"]; const getNext = (value, arr) => { const a = [undefined].concat(arr); const p = a.indexOf(value) + 1; ...

Read More

Complicated array grouping JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 233 Views

Suppose we have an array of objects like this − const arr = [ {userId: "3t5bsFB4PJmA3oTnm", from: 1, to: 6}, {userId: "3t5bsFB4PJmA3oTnm", from: 7, to: 15}, {userId: "3t5bsFB4PJmA3oTnm", from: 172, to: 181}, {userId: "3t5bsFB4PJmA3oTnm", from: 182, to: 190} ]; We need to write a JavaScript function that groups consecutive objects based on their "from" and "to" properties. Objects are considered consecutive when the "to" value of one object plus 1 equals the "from" value of the next object with the ...

Read More

Determine whether there is a pair of values in the array where the average of the pair equals to the target average in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 179 Views

We are required to write a JavaScript function that takes in an array of sorted integers and a target average as first and second arguments. The function should determine whether there is a pair of values in the array where the average of the pair equals to the target average. There's a solution with O(1) additional space complexity and O(n) time complexity. Since an array is sorted, it makes sense to have two indices: one going from begin to end (say y), another from end to begin of an array (say x). Algorithm Approach The two-pointer ...

Read More

Find the minimum and maximum values that can be calculated by summing exactly four of the five integers in JavaScript

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

Given an array of five positive integers, we are required to find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. The array is not sorted all the times. For example: const arr = [1, 3, 5, 7, 9] The minimum sum is: 1 + 3 + 5 + 7 = 16 and the maximum sum is: 3 + 5 + 7 ...

Read More

How to get the most common values in array: JavaScript ?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 489 Views

We are required to write a JavaScript function that takes in an array of literals that have repeating values. Our function should return an array of the most common element(s) in the array (if two or more elements appear for the same number of most times then the array should contain all those elements). Example The code for this will be − const arr1 = ["a", "c", "a", "b", "d", "e", "f"]; const arr2 = ["a", "c", "a", "c", "d", "e", "f"]; const getMostCommon = arr => { const count = ...

Read More

Grouping array nested value while comparing 2 objects - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 454 Views

When working with complex nested objects, you often need to compare and group data from different states. This article demonstrates how to group array nested values while comparing two objects in JavaScript. Problem Statement Suppose we have a JSON object containing "before" and "after" states of device data: const input = { "before": { "device": [ { "id": "1234", "price": "10", ...

Read More

Sort array of objects by string property value - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 366 Views

Sorting an array of objects by a string property is a common task in JavaScript. The most efficient approach uses the built-in sort() method with localeCompare() for proper alphabetical ordering. Sample Data Let's work with this array of person objects: const people = [ { first_name: 'Lazslo', last_name: 'Jamf' }, { first_name: 'Pig', last_name: 'Bodine' }, { first_name: 'Pirate', last_name: 'Prentice' } ]; console.log("Original array:", people); Original array: [ { first_name: 'Lazslo', last_name: 'Jamf' }, { ...

Read More
Showing 141–150 of 5,881 articles
« Prev 1 13 14 15 16 17 589 Next »
Advertisements