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

  • Home
  • SEARCH
  • 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 619811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:41:43+00:00 2026-05-13T18:41:43+00:00

Is there a way to merge(union without dupes) two given lists into one and

  • 0

Is there a way to merge(union without dupes) two given lists into one and store the items in sorted way by using ONE for loop?

Also, i am looking for a solution which does not makes use of API methods ( like, union, sort etc).

Sample Code.

private static void MergeAndOrder() 
{
var listOne = new List<int> {3, 4, 1, 2, 7, 6, 9, 11}; 
var listTwo = new List<int> {1, 7, 8, 3, 5, 10, 15, 12}; 

//Without Using C# helper methods...
//ToDo.............................

//Using C# APi.
var expectedResult = listOne.Union(listTwo).ToList(); 
expectedResult.Sort();//Output: 1,2,3,4,5,6,7,8,9,10,11,12,15
//I need the same result without using API methods, and that too by iterating over items only once.


}

PS: I have been asked this question in an interview, but couldn’t find answer as yet.

  • 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-13T18:41:44+00:00Added an answer on May 13, 2026 at 6:41 pm

    Without the precondition that both lists are sorted before the merge + sort operation, you can’t do this in O(n) time (or “using one loop”).

    Add that precondition and the problem is very easy.

    Keep two iterators, one for each list. On each loop, compare the element from each list and choose the smaller. Increment that list’s iterator. If the element you are about to insert in the final list is already the last element in that list, skip the insert.

    In pseudocode:

    List a = { 1, 3, 5, 7, 9 }
    List b = { 2, 4, 6, 8, 10 }
    List result = { }
    int i=0, j=0, lastIndex=0
    while(i < a.length || j < b.length)
        // If we're done with a, just gobble up b (but don't add duplicates)
        if(i >= a.length)
            if(result[lastIndex] != b[j])
                result[++lastIndex] = b[j]
            j++
            continue
    
        // If we're done with b, just gobble up a (but don't add duplicates)
        if(j >= b.length)
            if(result[lastIndex] != a[i])
                result[++lastIndex] = a[i]
            i++
            continue
    
        int smallestVal
    
        // Choose the smaller of a or b
        if(a[i] < b[j])
            smallestVal = a[i++]
        else
            smallestVal = b[j++]
    
        // Don't insert duplicates
        if(result[lastIndex] != smallestVal)
            result[++lastIndex] = smallestVal
    end while
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to merge two primary keys into one and then cascade
Is there any way to merge two users' history into one user in SVN?
Is there a better way to merge/collate a bunch of sorted iterators into one
Is there a way to merge two exe files into one, programmatically, so that
Using Subversion, is there a way to merge the changes from one specific revision
Is there a way to merge different String-Collections into one JSON 'String'? I would
In Git, is there a way to merge all changes from one branch into
Is there a way in C# to merge two dictionaries? I have two dictionaries
What would anyone consider the most efficient way to merge two datasets using Python?
Having two InputStreams in Java, is there a way to merge them so you

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.