借wangjianjun02 (啊龙)的帖子
http://community.csdn.net/Expert/TopicView3.asp?id=5470821有两个数组:int[] a = new int[]{1,2,3,4,5,6,7,8,9,10};
int[] b = new int[]{1,2,3,4,5,6,7,8};有什么方法快速的找出这两个数组中不同的数字?大家都动手写下  最后评测下谁的速度最快

解决方案 »

  1.   

    我先帖我的 抛砖引玉
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                int[] b = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };            Stopwatch sw = new Stopwatch();
                sw.Start();            //***********
                //算法开始
                //***********            //快排
                Array.Sort(a);
                Array.Sort(b);            List<int> list = new List<int>();            int a_len = a.Length;
                int b_len = b.Length;
                for (int i = 0,j=0; i < a_len || j < b_len ; )
                {
                    if (a[i] == b[j])
                    {
                        i++;
                        j++;
                    }
                    else if (a[i] < b[j])
                    {
                        list.Add(a[i]);
                        i++;
                    }
                    else
                    {
                        list.Add(b[j]);
                        j++;
                    }                //判断一组扫完的情况 即==的情况
                    if (i >= a.Length)
                    {
                        for (; j < b.Length; j++)
                        {
                            list.Add(b[j]);
                        }
                    }
                    if (j >= b.Length)
                    {
                        for (; i < a.Length; i++)
                        {
                            list.Add(a[i]);
                        }
                    }
                }            //******************
                //算法结束
                //******************
                sw.Stop();
                Console.WriteLine(sw.Elapsed.Ticks);
                for (int i = 0; i < list.Count; i++)
                    Console.Write("{0} ", list[i]);
                Console.Read();
            }
        }
    }
    结果 (Release模式)
    231 (时间)
    9 10
      

  2.   

    最后用大容量的int[]来比  肯定小不了  自己测试的时候用这个来看有错误没
      

  3.   

    两个都sort一下,再从头到尾扫一遍
      

  4.   

    hoho 我也借个地方这会没事