Object Oriented Programming Articles

Page 53 of 589

Find all disjointed intersections in a set of vertical line segments in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 194 Views

We have a set of vertical regions defined by y1 and y2 coordinates, where y1 is the starting point and y2 is the ending point of each region. The origin of our coordinates system is the top-left corner, so y2 is always greater than y1. This is an example: const regions = [ [10, 100], [50, 120], [60, 180], [140, 220] ]; console.log(regions); [ [ 10, 100 ], [ 50, 120 ], [ 60, 180 ], [ ...

Read More

Efficient algorithm for grouping elements and counting duplicates in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 394 Views

When working with arrays of objects in JavaScript, you often need to group elements based on shared properties and count duplicates. This is common in data processing tasks like analyzing user activity, inventory management, or survey results. Problem Overview Given an array of objects, we want to group them by a specific property and count how many times each group appears. For example, if we have objects with properties like coordinates or identifiers, we can group by the first property and display counts. Original data: X A B O Y X Z I Y ...

Read More

Most efficient method to groupby on an array of objects - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 324 Views

Grouping arrays of objects is a common task in JavaScript data processing. We'll explore efficient methods to group objects by single or multiple properties and aggregate values. Sample Data Let's start with an array of objects representing project phases, steps, and tasks: const arr = [ { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" }, { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" }, { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" ...

Read More

Get the smallest array from an array of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 547 Views

When working with nested arrays in JavaScript, you might need to find the smallest subarray based on the number of elements. This is useful for data processing, filtering operations, or when you need to identify the shortest path or sequence. Suppose we have a nested array of arrays like this: const arr = [ ["LEFT", "RIGHT", "RIGHT", "BOTTOM", "TOP"], ["RIGHT", "LEFT", "TOP"], ["TOP", "LEFT"] ]; console.log("Original array:", arr); Original array: [ [ 'LEFT', 'RIGHT', 'RIGHT', 'BOTTOM', 'TOP' ], ...

Read More

Sorting an array by price in JavaScript

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

Sorting an array of objects by price is a common requirement in JavaScript applications. This article demonstrates how to sort house data by price values stored as strings. Sample Data Let's start with an array of house objects where prices are stored as strings: const arr = [ { "h_id": "3", "city": "Dallas", "state": "TX", "zip": "75201", ...

Read More

Sort by index of an array in JavaScript

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

Sorting an array of objects by a specific property is a common task in JavaScript. This tutorial demonstrates how to sort objects by their index property and extract specific values. Problem Statement Suppose we have the following array of objects: const arr = [ { 'name': 'd', 'index': 3 }, { 'name': 'c', ...

Read More

Turning a 2D array into a sparse array of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 260 Views

Suppose, we have a 2-D array like this − const arr = [ [3, 1], [2, 12], [3, 3] ]; We are required to write a JavaScript function that takes in one such array. The function should then create a new 2-D array that contains all elements initialized to undefined other than the element's index present in the input array. Therefore, for the input array: output[3][1] = 1; output[2][12] = 1; output[3][3] = 1; And rest all elements ...

Read More

Alphanumeric sorting using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 471 Views

We have a mixed array that we need to sort by alphabet and then by digit. This requires a custom sorting function that handles both alphabetical and numerical parts correctly. const arr = ['Ab-1', 'Ab-11', 'Ab-12', 'ab-10', 'ab-100', 'ab-101', 'ab2', 'ab-3', 'ab-105']; console.log("Original array:", arr); Original array: [ 'Ab-1', 'Ab-11', 'Ab-12', 'ab-10', 'ab-100', 'ab-101', 'ab2', 'ab-3', 'ab-105' ] The Problem with Standard Sort JavaScript's default string sort doesn't handle numbers correctly. It treats "10" as smaller than "2" because it compares character ...

Read More

Enter a number and write a function that adds the digits together on button click in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 661 Views

We are required to write a JavaScript program that provides users an input to fill in a number. And upon filling when the user clicks the button, we should display the sum of all the digits of the number. HTML Structure First, let's create the HTML elements needed for our application: Submit JavaScript Implementation Here's the JavaScript function that calculates the sum of digits: function myFunc() { var num = document.getElementById('digits').value; var tot = 0; ...

Read More

JavaScript - summing numbers from strings nested in array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 225 Views

When working with arrays containing string numbers (like credit card numbers), you often need to extract and sum the numeric values. This article shows how to find the string with the greatest sum of digits. Problem Statement Given an array of credit card numbers as strings, we need to find the card with the highest sum of digits. If multiple cards have the same sum, return the last one encountered. const arr = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260']; Solution Approach The solution involves two main steps: extracting numbers from each string and calculating their ...

Read More
Showing 521–530 of 5,881 articles
« Prev 1 51 52 53 54 55 589 Next »
Advertisements