C# - 删除重复的行

时间:2018-03-24 21:16:03

标签: c#

我尝试删除两个不同ArrayList中的重复行。 这是我的代码:

 ArrayList URLs = new ArrayList();
 ArrayList Duplicated = new ArrayList();
 byte[] data = wc.DownloadData("https://www.bing.com/search?q=" + keyword);
                MatchCollection M = Regex.Matches(Encoding.UTF8.GetString(data, 0, data.Length), "[a-z]+[:][/][/][a-z]+[.][a-zA-Z0-9]+[.][a-z]+");
                foreach (Match m in M)
                    Duplicated.Add(m.Value);
                foreach (string line in Duplicated)
                    URLs.Add(line);
                for (int i = 0; i < Duplicated.Count; i++)
                {
                    if (URLs.Contains(Duplicated[i]))
                        URLs.Remove(Duplicated[i]);
                }
                foreach (string line in URLs)
                    richTextBox1.AppendText(line + "\r\n");

1 个答案:

答案 0 :(得分:0)

不是试图删除重复的,而是使用带有计数的字典,而只选择计数为1的项目。

 System.Collections.Generic.Dictionary<string, int> items = new System.Collections.Generic.Dictionary<string, int>();
 byte[] data = wc.DownloadData("https://www.bing.com/search?q=" + keyword);
 MatchCollection M = Regex.Matches(Encoding.UTF8.GetString(data, 0, data.Length), "[a-z]+[:][/][/][a-z]+[.][a-zA-Z0-9]+[.][a-z]+");
 foreach (Match m in M)
 {
     if ( items.ContainsKey(m.Value) )
         items[m.Value] += 1;
     else
         items.Add(m.Value, 1);
 }

 System.Collections.Generic.List<string> noDups = items.Where(b => b.Value == 1).Select(c => c.Key).ToList();