Posted: 10/8/2010
Hello Experts,
I have done the merging of 2 arrays in the below code. I have used List to store the values of both the arrays and finally used the Sort() method of List type to sort the elements.
Below is the sample.
using System; using System.Collections.Generic; namespace ArraySort { class Program { static void Main(string[] args) { int[] arr1 = {1, 2, 4, 6}; int[] arr2 = {3,4,6,7}; List<int> newList = new List<int>(); for (int i = 0; i < arr1.Length; i++) { if (!newList.Contains(arr1[i])) { newList.Add(arr1[i]); } } for (int j = 0; j < arr2.Length; j++) { if (!newList.Contains(arr2[j])) { newList.Add(arr2[j]); } } newList.Sort(); foreach (var list in newList) { Console.WriteLine(list); } Console.ReadLine(); } } }
I want to do the optimization of the code.
Can anyone tell me how can I do it?
Thanks,
Sumit
SumitArora said: user="Sumit Arora"]I want to do the optimization of the code.
user="Sumit Arora"]I want to do the optimization of the code.
Sumit,
You can do that via Linq use as follows:
public class SortArray { public static void Main() { int[] x ={1, 2, 4,6}; int[] y = {3,4, 6,7}; int[] z = x.Union(y).ToArray(); foreach (var i in z) { Console.WriteLine(i); } Console.ReadLine(); } }//end class
*********** Updated answer*************
public class SortArray { public static void Main() { int[] x ={1, 2, 4,6}; int[] y = {3,4, 6,7}; Array z = new Int32[x.Length + y.Length]; z = x.Union(y).ToArray(); Array.Sort(z); foreach (var i in z) { Console.WriteLine(i); } Console.ReadLine(); } }//end class
Vijjendra said: user="Vijendra Shakya"]You can do that via Linq use as follows: public class SortArray { public static void Main() { int[] x ={1, 2, 4,6}; int[] y = {3,4, 6,7}; int[] z = x.Union(y).ToArray(); foreach (var i in z) { Console.WriteLine(i); } Console.ReadLine(); } }//end class
user="Vijendra Shakya"]
Hello Vij,
Thanks for the answer.
This is a nice way of doing it through LINQ. But I have to do sorting of the elements as well and moreover I can't user LINQ as I'm still using Asp.Net 2.0. Do you know anyother way then do reply.
Many Thanks,
Hi,
Sumit please find out my updated post answer for sorting.
SumitArora said: user="Sumit Arora"]Hello Vij,Thanks for the answer. This is a nice way of doing it through LINQ. But I have to do sorting of the elements as well and moreover I can't user LINQ as I'm still using Asp.Net 2.0. Do you know anyother way then do reply.Many Thanks,Sumit
user="Sumit Arora"]
Hi Sumit,
Try the blew code:
class MergeTwoArray { static void Main(string[] args) { int[] x = { 1, 2, 4, 6 }; int[] y = { 3, 4, 6, 7 }; int[] z = new int[x.Length + y.Length]; x.CopyTo(z, 0); y.CopyTo(z, x.Length); Array.Sort(z); foreach (int i in z) { Console.WriteLine(i); } } }//end class
Hope it will help you.
@Sumit, If you want the end result in List that contains values of type Int, the following way would be the shortest:
int[] arr1 = { 1, 2, 4, 6 }; int[] arr2 = { 3, 4, 6, 7 }; List<int> newList = new List<int>(); newList.AddRange(arr1); newList.AddRange(arr2); newList.Sort();
Hope this helps.