How to Merge Two Lists in C#

rk042

Rathod Ketan

Posted on April 14, 2024

How to Merge Two Lists in C#

c# merge two list by interviewspreparation.com,merge two lists in c# gif

In intermediate-level interviews, candidates are frequently asked to 'combine', 'merge', 'join', or 'concatenate' two lists in C# programming. Alternatively, interviewers might rephrase the query to 'merge two sorted lists in C#'. Regardless of the exact wording, the expected response remains consistent. or if you have other interview questions follow us in Quora and ask any programing questions you want.

Recommended posts :
C++ Interview Questions for Beginners Most Asked Topics Part 1
Mastering Object-Oriented Programming in C++
Inheritance in OOPs: Ultimate Guide for Coding Interviews

If you are Unity Developer and instructed to learn ECS and DOTs check my new blog. where I will convert Non-ECS unity project to ECS project.

Understand Logic to Join Two Lists in C

Let's comprehend the logic behind joining two sorted lists or merging two lists in C#. In this program, we have two lists, A and B, both of which are already sorted. Let's assume that A = {2, 4, 6} and B = {1, 3, 5, 7, 9, 10}. Now, you have two choices to merge those lists. First, combine the lists into a single list and then sort it. Second approach is to first sort the lists by each element and simultaneously add the sorted elements to the list.

In this post, I am going to follow the second approach. To begin, create a loop to fetch each element from both lists. During this process, compare the elements from each list to determine which one is smaller. By doing so, both lists will merge into a single list.

In some cases, both lists have different element counts, so you have to add the remaining elements from each list using a loop.

It's quite confusing, isn't it? Don't worry. In the next section, I'll implement this logic in code so you can understand the rest of the process there.

Program to Merge Two Lists in C

Let's divide it into small steps to implement the logic clearly in the program. If you are a new visitor and not familiar with our work, I suggest checking out our Pattern, Pyramid, Number, and Array programs.



static void Main(string[] args)
{
    List<int> listA = new List<int> { 2, 4, 6 };
    List<int> listB = new List<int> { 1, 3, 5, 7, 9, 10 };

    List<int> mergedList = MergeSortedLists(listA, listB);

    foreach (var item in mergedList)
    {
        Console.Write(item + " ");
    }
}
static List<int> MergeSortedLists(List<int> listA, List<int> listB)
{
    List<int> outputList = new List<int>();

    int i = 0, j = 0;

    while (i < listA.Count && j < listB.Count)
    {
        if (listA[i] < listB[j])
        {
            outputList.Add(listA[i]);
            i++;
        }
        else
        {
            outputList.Add(listB[j]);
            j++;
        }
    }

    while (i < listA.Count)
    {
        outputList.Add(listA[i]);
        i++;
    }

    while (j < listB.Count)
    {
        outputList.Add(listB[j]);
        j++;
    }

    return outputList;
}


Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
rk042
Rathod Ketan

Posted on April 14, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How to Merge Two Lists in C#
programming How to Merge Two Lists in C#

April 14, 2024