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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:20:03+00:00 2026-05-22T01:20:03+00:00

I need to intersect 2 arrays ( List<T> ) by keeping just the distinct

  • 0

I need to intersect 2 arrays (List<T>) by keeping just the distinct elements and mix the elements of one array with the other like this:

    A1 B1 C1 D1
    W2 X2 Y2 Z2 
 => A1 W2 B1 X2 C1 Y2 D1 Z2


    A1 B1 A1 D1
    W2 X2 Y2 Z2 
 => A1 W2 B1 X2 Y2 D1 Z2 // repeating A1


    A1 B1 A1 D1
    Z2 X2 Y2 Z2 
 => A1 Z2 B1 X2 Y2 D1  // repeating A1, Z2

    A1 B1 C1 D1 E1
    Z2 X2 Y2 
 => A1 Z2 B1 X2 C1 Y2 D1 E1

What is the optimal LINQ query to do it?

  • 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-22T01:20:03+00:00Added an answer on May 22, 2026 at 1:20 am

    An interleave extension method (replaces Zip/SelectMany in Jon Skeets version). This may be a little faster and it includes the remaining of the longest list if the lists aren’t of equal length:

    public static IEnumerable<T> Interleave<T>(this IEnumerable<T> first, IEnumerable<T> second)
    {
        var e0 = first.GetEnumerator();
        var e1 = second.GetEnumerator();
    
        bool e0HasValue = false;
        bool e1HasValue = false;
    
        // Could be made a little faster by not checking both when one is empty
        while ((e0HasValue = e0.MoveNext()) | (e1HasValue = e1.MoveNext()))
        {
            if (e0HasValue)
                yield return e0.Current;
    
            if (e1HasValue)
                yield return e1.Current;
        }
    }
    

    And if you want distinct items, use:

    var result = firstList.Interleave(secondList).Distinct();
    

    This answer, of cause, doesn’t solve the uncertainty about Distinct as described in comments to Jon Skeet´s answer. But it could be combined with Jon Skeet´s order preserving extension method.

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

Sidebar

Related Questions

I have 2 arrays which might look something like this: $a1 = array('c','b','a'); $a2
I have an array that looks like this (the full list is here ):
I have a multidimensional array that looks like this: Array ( [0] => Array
I need to change the firefox window title like I would do for internet
I need to use (not implement) an array based version of Dijkstras algo .The
I have two arrays I want to combine. I need to take the values
I need a way to merge an array of rectangle objects (objects with x,y,w,h
Given a list of objects with multiple attributes I need to find the list
I need to find all sub-arrays which share any mutual element and merge them
I have an application with tabs. In one tab I need to put data

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.