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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:34:21+00:00 2026-06-14T17:34:21+00:00

If I have, for example the following List<int> s { 1, 2, 3, 4

  • 0

If I have, for example the following List<int>s

{ 1, 2, 3, 4 } //list1
{ 2, 3, 5, 6 } //list2
...
{ 3, 4, 5 }    //listN

What is the best way to retrieve the following corresponding List<int?>s?

{    1, 2,    3,    4,    null, null } //list1
{ null, 2,    3,    null, 5,    6    } //list2
...
{ null, null, 3,    4,    5,    null } //listN
  • 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-06-14T17:34:22+00:00Added an answer on June 14, 2026 at 5:34 pm

    I’m posting the solution we discussed in chat. I had an unoptimized version using Linq for all things loopy/filtering:

    • http://ideone.com/H4gCoE (live demo)

    However, I suspect it won’t be too performant because of all the enumerator classes created, and the collections being instantiated/modified along the way.

    So I took the time to optimize it into handwritten loops with an administration to keep track of active iterators instead of modifying the iters collection. Here it is:

    See http://ideone.com/FuZIDy for full live demo.

    Note I assume the lists are pre-ordered by DefaultComparer<T>, since I use Linq’sMin() extension method without a custom comparer

    public static IEnumerable<IEnumerable<T>> AlignSequences<T>(this IEnumerable<IEnumerable<T>> sequences)
    {
        var iters = sequences
            .Select((s, index) => new { active=true, index, enumerator = s.GetEnumerator() })
            .ToArray();
    
        var isActive = iters.Select(it => it.enumerator.MoveNext()).ToArray();
        var numactive = isActive.Count(flag => flag);
    
        try
        {
            while (numactive > 0)
            {
                T min = iters
                    .Where(it => isActive[it.index])
                    .Min(it => it.enumerator.Current);
    
                var row = new T[iters.Count()];
    
                for (int j = 0; j < isActive.Length; j++)
                {
                    if (!isActive[j] || !Equals(iters[j].enumerator.Current, min)) 
                        continue;
    
                    row[j] = min;
                    if (!iters[j].enumerator.MoveNext())
                    {
                        isActive[j] = false;
                        numactive -= 1;
                    }
                }
                yield return row;
            }
        }
        finally
        {
            foreach (var iter in iters) iter.enumerator.Dispose();
        }
    }
    

    Use it like this:

    public static void Main(string[] args)
    {
        var list1 = new int?[] { 1, 2, 3, 4, 5 };
        var list2 = new int?[] { 3, 4, 5, 6, 7 };
        var list3 = new int?[] { 6, 9, 9 };
    
        var lockstep = AlignSequences(new[] { list1, list2, list3 });
    
        foreach (var step in lockstep)
            Console.WriteLine(string.Join("\t", step.Select(i => i.HasValue ? i.Value.ToString() : "null").ToArray()));
    }
    

    It prints (for demo purposes I print the results sideways):

    1       null    null
    2       null    null
    3       3       null
    4       4       null
    5       5       null
    null    6       6
    null    7       null
    null    null    9
    null    null    9
    

    Note: You might like to change the interface to accept arbitrary number of lists, instead of a single sequence of sequences:

    public static IEnumerable<IEnumerable<T>> AlignSequences<T>(params IEnumerable<T>[] sequences)
    

    That way you could just call

    var lockstep = AlignSequences(list1, list2, list3);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following example, compiled in VS2005, warning level 4: int main(int argc,
Suppose I have the following: typedef struct { int itemSize; int count; void *list;
Take the following example: private int[] list; public Listing() { // Why can't I
I'm trying to figure out how XSLT process namespace prefixes and have following example:
We have the following example in node.js var http = require('http'); http.createServer(function(request, response) {
I have the following example code: $dataProvider = new CActiveDataProvider('firstTable', array('criteria' => array( 'select'
I have the following example of reading from a buffered reader: while ((inputLine =
I have the following example in a SQL table Cust Group Sales A 1
I have the following example of what a user might type into a field
In the following example I have two years worth of data denoted by data_2007

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.