Loading ...

How to do the optimization Array merging code?

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » How to do the optimization Array merging code?

How to do the optimization Array merging code?

Posts under the topic: How to do the optimization Array merging code?

Posted: 10/8/2010

Starter 1414  points  Starter
  • Joined on: 12/1/2008
  • Posts: 66

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


Posted: 10/8/2010

Contributor 2841  points  Contributor
  • Joined on: 11/29/2008
  • Posts: 62
  Answered

 

SumitArora said:

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



tags ASP.NET, LINQ, C#, Array

Posted: 10/8/2010

Starter 1414  points  Starter
  • Joined on: 12/1/2008
  • Posts: 66

 

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

 

 

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

 


Posted: 10/8/2010

Contributor 2841  points  Contributor
  • Joined on: 11/29/2008
  • Posts: 62

Hi,

Sumit please find out my updated post answer for sorting.

Posted: 10/8/2010

Starter 695  points  Starter
  • Joined on: 6/17/2009
  • Posts: 14

 

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

 

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.


Posted: 10/8/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391
  Answered

@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.


Page 1 of 1 (6 items)