How to Merge Two Lists in C#
Tags: programming, tutorial, career, interview
Published: 2024-04-14 07:13:00


![c# merge two list by interviewspreparation.com,merge two lists in c# gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a4p37v6z3tky9clon624.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#](https://interviewspreparation.com/how-to-merge-two-lists-in-csharp/)'. Regardless of the exact wording, the expected response remains consistent. or if you have other interview questions follow us in [Quora ](https://interviewspreparation.com/how-to-merge-two-lists-in-csharp/)and ask any programing questions you want.

Recommended posts :
[Mastering Object-Oriented Programming in C++](https://interviewspreparation.com/understanding-object-oriented-programming-oop-in-cpp/)

## 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](https://interviewspreparation.com/tag/pattern/), [Pyramid](https://interviewspreparation.com/tag/pyramid/), [Number](https://interviewspreparation.com/tag/number/), and [Array ](https://interviewspreparation.com/tag/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;
}
```