对于网上有针对查找数组第一个不重复数字的效率问题

网上有人说用查找用字典比较好。我就拿了字典与传统的循环比较。因为字典虽然比较快 但是数组需要写入字典在进行查找 我就在想这个比较慢吧

 

public static void Main(string[] args)
{
System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
stopwatch.Restart();
UseByDictionaryByWhere();
stopwatch.Stop();
Console.WriteLine("UseByDictionaryByWhere()所用时间:" + stopwatch.Elapsed);
stopwatch.Restart();
UseByDictionaryForeach();
stopwatch.Stop();
Console.WriteLine("UseByDictionaryForeach()所用时间:" + stopwatch.Elapsed);
stopwatch.Restart();
UseByDictionary();
stopwatch.Stop();
Console.WriteLine("UseByDictionary()所用时间:" + stopwatch.Elapsed);
stopwatch.Restart();
int a = UseByFor();
stopwatch.Stop();
Console.WriteLine("UseByFor()所用时间:" + stopwatch.Elapsed);
Console.ReadLine();

}

public static void UseByDictionaryByWhere()
{
int[] list = new int[] { 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 7, 3, 2, 8 };
//Console.Write("原数组:");
//foreach (int item in list)
//{
// Console.Write(item + " ");
//}
//Console.WriteLine();
//方法一:遍历数组,存到字典中
Dictionary<int, int> dic = new Dictionary<int, int>();
foreach (int item in list)
{
//如果字典中不存在key为当前item的值的对象
if (!dic.ContainsKey(item))
{
//把当前数组元素添加到字典中(key为当前item,值为1)
dic[item] = 1;
}
else
{
//字典中存在key为当前数组元素的对象,把这个对象的value加1
dic[item]++;
}
}
var a = dic.Where(e => e.Value == 1).FirstOrDefault();

//var a = dic.FirstOrDefault(e => e.Value == 1);

}
public static void UseByDictionaryForeach()
{
int[] list = new int[] { 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 7, 3, 2, 8 };
//Console.Write("原数组:");
//foreach (int item in list)
//{
// Console.Write(item + " ");
//}
//Console.WriteLine();
//方法一:遍历数组,存到字典中
Dictionary<int, int> dic = new Dictionary<int, int>();
foreach (int item in list)
{
//如果字典中不存在key为当前item的值的对象
if (!dic.ContainsKey(item))
{
//把当前数组元素添加到字典中(key为当前item,值为1)
dic[item] = 1;
}
else
{
//字典中存在key为当前数组元素的对象,把这个对象的value加1
dic[item]++;
}
}
foreach (KeyValuePair<int, int> kvp in dic)
{
if (kvp.Value == 1)
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
break;
}
}
//var a = dic.Where(e => e.Value == 1).FirstOrDefault();
//var a = dic.FirstOrDefault(e => e.Value == 1);

}
public static void UseByDictionary()
{
int[] list = new int[] { 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 7, 3, 2, 8 };
//Console.Write("原数组:");
//foreach (int item in list)
//{
// Console.Write(item + " ");
//}
//Console.WriteLine();
//方法一:遍历数组,存到字典中
Dictionary<int, int> dic = new Dictionary<int, int>();
foreach (int item in list)
{
//如果字典中不存在key为当前item的值的对象
if (!dic.ContainsKey(item))
{
//把当前数组元素添加到字典中(key为当前item,值为1)
dic[item] = 1;
}
else
{
//字典中存在key为当前数组元素的对象,把这个对象的value加1
dic[item]++;
}
}

var a = dic.FirstOrDefault(e => e.Value == 1);

 

public static int UseByFor()
{
int[] list = new int[] { 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 7, 3, 2, 8 };
int readyNum = 0;
int num = 0;
for (int i = 0; i < list.Count(); i++)
{
for (int j = 0; j < list.Count(); j++)
{
num = 0;
if (list[j] == list[i])
{
num++;
}
readyNum = list[i];
}
//if (num == 1)
// return readyNum;
}
return readyNum;
}

  

 

可以看出传统转成字典在用字典的foreach 是比较快速的查询

再大量数的时候

   public static void Main(string[] args)
        {
            System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
            stopwatch.Restart();
            UseByDictionaryByWhere();
            stopwatch.Stop();
            Console.WriteLine("UseByDictionaryByWhere()所用时间:" + stopwatch.Elapsed);
            stopwatch.Restart();
            UseByDictionaryForeach();
            stopwatch.Stop();
            Console.WriteLine("UseByDictionaryForeach()所用时间:" + stopwatch.Elapsed);
            stopwatch.Restart();
            UseByDictionary();
            stopwatch.Stop();
            Console.WriteLine("UseByDictionary()所用时间:" + stopwatch.Elapsed);
            stopwatch.Restart();
            int a = UseByFor();
            stopwatch.Stop();
            Console.WriteLine("UseByFor()所用时间:" + stopwatch.Elapsed);
            Console.ReadLine();

        }

        public static void UseByDictionaryByWhere()
        {
            int[] list = new int[] { 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 7, 3, 2, 8 };
            //int[] list = new int[] { 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 7, 3, 2, 8 };
            //Console.Write("原数组:");
            //foreach (int item in list)
            //{
            //    Console.Write(item + " ");
            //}
            //Console.WriteLine();
            //方法一:遍历数组,存到字典中
            Dictionary<int, int> dic = new Dictionary<int, int>();
            foreach (int item in list)
            {
                //如果字典中不存在key为当前item的值的对象
                if (!dic.ContainsKey(item))
                {
                    //把当前数组元素添加到字典中(key为当前item,值为1)
                    dic[item] = 1;
                }
                else
                {
                    //字典中存在key为当前数组元素的对象,把这个对象的value加1
                    dic[item]++;
                }
            }
            var a = dic.Where(e => e.Value == 1).FirstOrDefault();

            //var a = dic.FirstOrDefault(e => e.Value == 1);



        }
        public static void UseByDictionaryForeach()
        {
            int[] list = new int[] { 1,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6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 7, 3, 2, 8 };
            //Console.Write("原数组:");
            //foreach (int item in list)
            //{
            //    Console.Write(item + " ");
            //}
            //Console.WriteLine();
            //方法一:遍历数组,存到字典中
            Dictionary<int, int> dic = new Dictionary<int, int>();
            foreach (int item in list)
            {
                //如果字典中不存在key为当前item的值的对象
                if (!dic.ContainsKey(item))
                {
                    //把当前数组元素添加到字典中(key为当前item,值为1)
                    dic[item] = 1;
                }
                else
                {
                    //字典中存在key为当前数组元素的对象,把这个对象的value加1
                    dic[item]++;
                }
            }
            foreach (KeyValuePair<int, int> kvp in dic)
            {
                if (kvp.Value == 1)
                {
                    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
                    break;
                }
            }
            //var a = dic.Where(e => e.Value == 1).FirstOrDefault();
            //var a = dic.FirstOrDefault(e => e.Value == 1);



        }
        public static void UseByDictionary()
        {
            int[] list = new int[] { 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4,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3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 7, 3, 2, 8 };
            //int[] list = new int[] { 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 7, 3, 2, 8 };
            //Console.Write("原数组:");
            //foreach (int item in list)
            //{
            //    Console.Write(item + " ");
            //}
            //Console.WriteLine();
            //方法一:遍历数组,存到字典中
            Dictionary<int, int> dic = new Dictionary<int, int>();
            foreach (int item in list)
            {
                //如果字典中不存在key为当前item的值的对象
                if (!dic.ContainsKey(item))
                {
                    //把当前数组元素添加到字典中(key为当前item,值为1)
                    dic[item] = 1;
                }
                else
                {
                    //字典中存在key为当前数组元素的对象,把这个对象的value加1
                    dic[item]++;
                }
            }
            // var a = dic.Where(e => e.Value == 1).FirstOrDefault();

            var a = dic.FirstOrDefault(e => e.Value == 1);



        }
        public static int UseByFor()
        {
            int[] list = new int[] { 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 3, 3, 2, 4, 5, 6, 5, 6, 1, 1, 2, 3, 1, 2, 4, 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 4, 5, 6, 5, 6, 7, 3, 2, 8 };
            int readyNum = 0;
            int num = 0;
            for (int i = 0; i < list.Count(); i++)
            {
                for (int j = 0; j < list.Count(); j++)
                {
                    num = 0;
                    if (list[j] == list[i])
                    {
                        num++;
                    }
                    readyNum = list[i];
                }
                if (num == 1)
                    return readyNum;
            }
            return readyNum;
        }

  

posted @ 2015-04-21 10:03  gclearn  阅读(313)  评论(0编辑  收藏  举报