Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6780175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:28:28+00:00 2026-05-26T16:28:28+00:00

var items = [ {id: 1, begin:10, end:30}, {id: 2, begin:20, end:40}, {id: 3,

  • 0
var items = [
    {"id": 1, "begin":10, "end":30},
    {"id": 2, "begin":20, "end":40},
    {"id": 3, "begin":30, "end":50},
    {"id": 4, "begin":40, "end":60},
    {"id": 5, "begin":50, "end":70},
    {"id": 6, "begin":100, "end":170},
    {"id": 7, "begin":120, "end":180}
];

for (var i = 0; i < items.length; i++) {
    var currBegin = items[i].begin;
    var currEnd   = items[i].end;
    var out = [];
    for (var j = 0; j < items.length; j++) {
        var innerBegin = items[j].begin;
        var innerEnd = items[j].end;
        if (innerBegin < currBegin && innerEnd < currEnd && innerEnd >= currBegin)
          out.push(items[j].id);
    }
    console.log(out)           
}

I’m trying to check if numbers overlap each other based on an initial and end number.
So far my code is giving the wrong output: http://jsfiddle.net/bZzSV/

[]
[1]
[1, 2]
[2, 3]
[3, 4]
[]
[6]

Basically check if an element overlaps with another. 1,2 overlap, but 1,3 doesn’t, however 2,3 does… at the end, 1-5 are overlapped because of those links.

I would like to output:

[1, 2, 3, 4, 5]
[6, 7]

Any help is appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:28:29+00:00Added an answer on May 26, 2026 at 4:28 pm

    If you do not care about destroying the original items object you can use these changes

    while (items.length > 0) {
        var curr = items.shift();
        var currBegin = curr.begin;
        var currEnd   = curr.end;
        var out = [curr.id], j = 0;
        while (j < items.length) {
            var innerBegin = items[j].begin;
            var innerEnd = items[j].end;
            if (innerBegin <= currBegin && innerEnd >= currBegin || innerEnd >= currEnd && innerBegin <= currEnd) {
              out.push(items[j].id);
              currBegin = Math.min(currBegin, innerBegin);
              currEnd = Math.max(currEnd, innerEnd);
              items.splice(j, 1);
            } else {
              j++;
            }
        }
        console.log(out)           
    }
    

    but it relies on several premises like that the overlapping intervals are adjacent.

    1. The main problem in your code is that you do not update the bounds of the currBegin currEnd interval when adding to it.

    2. Other problem is the detection of overlapping intervals.

    3. And the last problem is not remembering which items were already put to some out array (this is solved here by simply deleting the items, which may not be ideal, but can be done otherwise if needed).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

See the code: function ShowItem(MyData: TMyData): TListItem; var vListItem: TListItem; begin vListItem := ListView1.Items.Add;
I have a basic NHibernate.Linq query: var items = from item in session.Linq<ObjectType>() where
Having : var Difference: DWORD // difference shows in milliseconds // List.Items.Count can be
Imagine that during a foreach(var item in enumerable) The enumerable items change. It will
I need get all items these have no categories int? categoryId = null; var
this code works fine: procedure TForm2.Timer1Timer(Sender: TObject); var Text: string; begin SetLength (Text,555); GetWindowText
I have a case using TDictionary: var D: TDictionary<string, integer>; begin D := TDictionary<string,
So, my end game here is to have a way to list items (like
I have the following Delphi code: destructor TXX_XXXX.Destroy; var i: Integer; begin if Assigned(Allocations)
string x; foreach(var item in collection) { x += item+,; } can I write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.