實現足夠大List剔除重複的數字

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. 對比前後兩條數據,相同的調過,不同的保留下來






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章