
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# program to merge two sorted arrays into one
Set two arrays that you wish to merge −
int[] arr1 = new int[5] { 5, 15, 25, 30, 47 }; int[] arr2 = new int[5] { 55, 60, 76, 83, 95 };
Now take a third array that would merge both the above arrays −
int[] merged = new int[10];
The following is the code that merges two arrays into the third array in C# −
Example
using System; using System.Collections.Generic; class Program { static void Main() { int i = 0; int j = 0; int[] arr1 = new int[5] { 5, 15, 25, 30, 47 }; int[] arr2 = new int[5] { 55, 60, 76, 83, 95 }; int[] merged = new int[10]; for (i = 0, j = 0; i < 5; i++) { merged[j++] = arr1[i]; } for (i = 0; i < 5; i++) { merged[j++] = arr2[i]; } Console.WriteLine("Elements of merged array = "); for (i = 0; i < 10; i++) { Console.WriteLine("merged[" + (i) + "]: " + merged[i]); } Console.WriteLine(); } }
Output
Elements of merged array = merged[0]: 5 merged[1]: 15 merged[2]: 25 merged[3]: 30 merged[4]: 47 merged[5]: 55 merged[6]: 60 merged[7]: 76 merged[8]: 83 merged[9]: 95
- Related Articles
- Merge two sorted arrays into a list using C#
- Merge two sorted arrays in C#
- Merge two sorted arrays using C++.
- Merge two sorted arrays in Java
- Merging two sorted arrays into one sorted array using JavaScript
- Merge two sorted arrays to form a resultant sorted array in JavaScript
- Merge two sorted arrays in Python using heapq?
- 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)
- Program to merge two sorted list to form larger sorted list in Python
- Merge two sorted linked lists using C++.
- How to merge two arrays with objects in one in JavaScript?
- Merge two arrays using C# AddRange() method
- C/C++ Program for Median of two sorted arrays of same size?

Advertisements