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 992187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:13:35+00:00 2026-05-16T06:13:35+00:00

I was looking for some fast and efficient method do merge items in array.

  • 0

I was looking for some fast and efficient method do merge items in array. This is my scenario. The collection is sorted by From. Adjacent element not necessarily differ by 1, that is there can be gaps between the last To and the next From, but they never overlap.

var list = new List<Range>();
list.Add(new Range() { From = 0, To = 1, Category = "AB" });
list.Add(new Range() { From = 2, To = 3, Category = "AB" });
list.Add(new Range() { From = 4, To = 5, Category = "AB" });
list.Add(new Range() { From = 6, To = 8, Category = "CD" });
list.Add(new Range() { From = 9, To = 11, Category = "AB" }); // 12 is missing, this is ok
list.Add(new Range() { From = 13, To = 15, Category = "AB" });

I would like the above collection to be merged in such way that the first three (this number can vary, from at least 2 elements to as many as the condition is satisfied) elements become one element. Cannot merge elements with different category.

new Range() { From = 0, To = 5, Category = "AB" };

So that the resulting collection would have 4 elements total.

0 - 5    AB
6 - 8    CD
9 - 11   AB // no merging here, 12 is missing
13 - 15  AB

I have a very large collection with over 2.000.000 items and I would like to this as efficiently as possible.

  • 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-16T06:13:36+00:00Added an answer on May 16, 2026 at 6:13 am

    Here’s a generic, reusable solution rather than an ad hoc, specific solution.
    (Updated based on comments)

    IEnumerable<T> Merge<T>(this IEnumerable<T> coll, 
                          Func<T,T,bool> canBeMerged, Func<T,T,T>mergeItems)
    {
        using(IEnumerator<T> iter = col.GetEnumerator())
        {
          if (iter.MoveNext())
          {
              T lhs = iter.Current;
              while(iter.MoveNext())
              {
                  T rhs = iter.Current;
                  if (canBeMerged(lhs, rhs)
                     lhs=mergeItems(lhs, rhs);
                  else
                  {
                     yield return lhs;
                     lhs= rhs;
                  }
              }
              yield return lhs;
          }
        }
    }
    

    You will have to provide method to determine if the item can be merged, and to merge them.
    These really should be part of your Range class, so it would be called like them:

    list.Merge((l,r)=> l.IsFollowedBy(r), (l,r)=> l.CombineWith(r));
    

    If you don’t have these method, then you would have to call it like:

    list.Merge((l,r)=> l.Category==r.Category && l.To +1 == r.From,
               (l,r)=> new Range(){From = l.From, To=r.To, Category = l.Category});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a fast and efficient way to create pdf from my
I am looking for a fast and efficient protocol that can be used between
I am looking for some recommendations about compressing data in .NET, aside from using
Looking for some others thoughts on this cause I couldn't come up with anything
I'm looking for a Java lib that permits to do some fast computations with
From looking at the way some forum softwares are storing data in a database
I'm looking for some very clever and fast way of transforming every first character
I'm fiddling with a bit of c++/opencv. I was looking some of the samples
Looking at some assembly code for x86_64 on my Mac, I see the following
Looking at some of the code System.Linq I've come across some examples of Buffer<TSource>

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.