Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
How to check duplicate number range element in JavaScript array?

For example,

[{
    "numberStart": "300",
    "numberEnd": "350",
    "id": "1"
}, {
    "numberStart": "351",
    "numberEnd": "400",
    "id": "2"
}, {
    "numberStart": "380", 
    "numberEnd": "400",
    "id": "3"
}]

In the above example, the third element of the array is duplicate because numberStart and numberEnd ranges already exist in the 2nd element of the array. How to find the duplicate element?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Assume you start with an empty list and you keep adding objects range to it.

One possible solution is the following. The new objects will be added only if they do not overlap the max range of the existing elements.

In the example below only the objects 1, 2, and 5 will be added to the array.

let list = [];   // start with an empty list
//--------------------------------
function addToList(list , toAdd) {
const maxValue = Math.max(...list.map(o => o.numberEnd), 0);
if (toAdd.numberEnd > maxValue) list=[{...list,...toAdd}];
return list;
}
//--------------------------------

list = addToList(list,{"numberStart": 300,"numberEnd": 350, "id": "1"});  // will be added
list = addToList(list,{"numberStart": 351,"numberEnd": 400, "id": "2"});  // will be added
list = addToList(list,{"numberStart": 380, "numberEnd": 400, "id": "3"}); // it will not be added
list = addToList(list,{"numberStart": 200, "numberEnd": 300, "id": "4"}); // it will not be added
list = addToList(list,{"numberStart": 401, "numberEnd": 500, "id": "5"}); // will be added

console.log(list);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!