- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose, we have two arrays of numbers that represents two ranges like these −

const arr1 = [2, 5]; const arr2 = [4, 7];

We are required to write a JavaScript function that takes in two such arrays.

The function should then create a new array of range, that is the intersection of both the input ranges and return that range.

Therefore, the output for the above input should look like this −

const output = [4, 5];

The code for this will be −

const arr1 = [2, 5]; const arr2 = [4, 7]; const findRangeIntersection = (arr1 = [], arr2 = []) => { const [el11, el12] = arr1; const [el21, el22] = arr2; const leftLimit = Math.max(el11, el21); const rightLimit = Math.min(el12, el22); return [leftLimit, rightLimit]; }; console.log(findRangeIntersection(arr1, arr2));

And the output in the console will be −

[ 4, 5 ]

- Related Questions & Answers
- Intersection of two arrays JavaScript
- Get the intersection of two sets in Java
- How to find intersection between two Numpy arrays?
- How to get the intersection of two arrays in MongoDB?
- Python - Fetch columns between two Pandas DataFrames by Intersection
- Python - Intersection of two String
- Intersection of two arrays in C#
- Intersection of two arrays in Java
- Intersection of two HashSets in C#
- Intersection of Two Arrays in C++
- How to get the difference between two arrays in JavaScript?
- How to get substring between two similar characters in JavaScript?
- How to find the intersection between two or more lists in R?
- Intersection of Two Linked Lists in Python
- Intersection of Two Arrays II in Python

Advertisements