
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding intersection of arrays of intervals in JavaScript
Problem
JavaScript function that takes in two arrays, arr1 and arr2 of intervals which are pairwise disjoint and in sorted order.
A closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b.
The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].) Our function is supposed to return the intersection of these two interval arrays.
For example, if the input to the function is −
const arr1 = [[0,2],[5,10],[13,23],[24,25]]; const arr2 = [[1,5],[8,12],[15,24],[25,26]];
Then the output should be −
const output = [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]];
Example
The code for this will be −
const arr1 = [[0,2],[5,10],[13,23],[24,25]]; const arr2 = [[1,5],[8,12],[15,24],[25,26]]; const findIntersection = function (A, B) { const res = [] let i = 0 let j = 0 while (i < A.length && j < B.length) { const [a, b] = A[i] const [c, d] = B[j] const lo = Math.max(a, c) const hi = Math.min(b, d) if (lo <= hi) { res.push([Math.max(a, c), Math.min(b, d)]) } if (b < d) { i++ } else { j++ } } return res }; console.log(findIntersection(arr1, arr2));
Output
And the output in the console will be −
[ [ 1, 2 ], [ 5, 5 ], [ 8, 10 ], [ 15, 23 ], [ 24, 24 ], [ 25, 25 ] ]
- Related Articles
- Finding intersection of multiple arrays - JavaScript
- Finding the intersection of arrays of strings - JavaScript
- Finding intersection of arrays that contain repetitive entries in JavaScript
- Unique intersection of arrays in JavaScript
- Intersection of two arrays JavaScript
- Intersection of three sorted arrays in JavaScript
- Find Intersection of all Intervals in C++
- Finding the inclination of arrays in JavaScript
- Finding reversed index of elements in arrays - JavaScript
- Finding the continuity of two arrays in JavaScript
- JavaScript Program for Finding Intersection of Two Sorted Linked Lists
- JavaScript Program for Finding Intersection Point of Two Linked Lists
- Intersection of two arrays in Java
- Intersection of two arrays in C#
- Intersection of Two Arrays in C++

Advertisements