- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Merge two sorted arrays in C#
To merge two sorted arrays, firstly set two sorted arrays −
int[] array1 = { 1, 2 }; int[] array2 = { 3, 4 };
Now, add it to a list and merge −
var list = new List<int>(); for (int i = 0; i < array1.Length; i++) { list.Add(array1[i]); list.Add(array2[i]); }
Use the ToArray() method to convert back into an array −
int[] array3 = list.ToArray();
The following is the complete code −
Example
using System; using System.Collections.Generic; public class Program { public static void Main() { int[] array1 = { 1, 2 }; int[] array2 = { 3, 4 }; var list = new List<int>(); for (int i = 0; i < array1.Length; i++) { list.Add(array1[i]); list.Add(array2[i]); } int[] array3 = list.ToArray(); foreach(int res in array3) { Console.WriteLine(res); } } }
Output
1 3 2 4
- Related Articles
- Merge two sorted arrays using C++.
- Merge two sorted arrays in Java
- Merge two sorted arrays into a list using C#
- C# program to merge two sorted arrays into one
- Merge two sorted arrays in Python using heapq?
- Merge two sorted arrays to form a resultant sorted array in JavaScript
- Merge k sorted arrays in Java
- Merge k sorted arrays of different sizes in C++
- Quickly merging two sorted arrays using std::merge() in C++ STL(cute ho ap)
- Merge two sorted linked lists using C++.
- Median of Two Sorted Arrays in C++
- Merge Two Sorted Lists in Python
- Merge two arrays using C# AddRange() method
- Merging two unsorted arrays in sorted order in C++.
- Find relative complement of two sorted arrays in C++

Advertisements