实现足够大List<int>剔除重复的数字

来源:互联网 发布:软件定义 编辑:程序博客网 时间:2024/05/22 12:27
static void Screen()    {        List<int> newList=new List<int>();        List<int> list =new List<int>();        for (int i = 1; i < 100000; i++)        {            list.Add(i%100);        }        int repetition = 0;        DateTime ts = System.DateTime.Now;        list.Sort();        int max = list.Count;        int _temp = list[0];        for (int i = 1; i < max; i++)        {            if (_temp.Equals(list[i]))            {                repetition++;                continue;            }            newList.Add(_temp);            _temp = list[i];        }        //添加最后一条        newList.Add(_temp);        TimeSpan tSpan = System.DateTime.Now - ts;        Debug.Log("消耗时间:"+tSpan.Milliseconds);        Debug.Log("重复:"+repetition);        Debug.Log("正确个数:"+newList.Count);    }


消耗时间:90
重复:99899
正确个数:100


实现思路是:

1. 先对这个数据排序。
2. 对比前后两条数据,相同的调过,不同的保留下来






0 0