【问题标题】:How to remove duplicates from List<string> without LINQ? [duplicate]如何在没有 LINQ 的情况下从 List<string> 中删除重复项? [复制]
【发布时间】:2011-11-07 15:17:05
【问题描述】:

可能重复:
Remove duplicates from a List<T> in C#

我有一个如下列表(电子邮件列表如此之大):
源列表:

item 0 : jumper@yahoo.com|32432  
item 1 : goodzila@yahoo.com|32432|test23  
item 2 : alibaba@yahoo.com|32432|test65  
item 3 : blabla@yahoo.com|32432|test32 

每个项目的重要部分是电子邮件地址,其他部分(用管道分隔不重要)但我想将它们保留在最终列表中。
正如我所说,我的清单很大,我认为不建议使用其他清单。

如何在不使用 LINQ 的情况下从列表中删除重复的电子邮件(整个项目)?
我的代码如下:

private void WorkOnFile(UploadedFile file, string filePath)
{
    File.SetAttributes(filePath, FileAttributes.Archive);

    FileSecurity fSecurity = File.GetAccessControl(filePath);
    fSecurity.AddAccessRule(new FileSystemAccessRule(@"Everyone",
                                                    FileSystemRights.FullControl,
                                                    AccessControlType.Allow));
    File.SetAccessControl(filePath, fSecurity);

    string[] lines = File.ReadAllLines(filePath);
    List<string> list_lines = new List<string>(lines);
    var new_lines = list_lines.Select(line => string.Join("|", line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)));
    List<string> new_list_lines = new List<string>(new_lines);
    int Duplicate_Count = 0;
    RemoveDuplicates(ref new_list_lines, ref Duplicate_Count);
    File.WriteAllLines(filePath, new_list_lines.ToArray());
}

private void RemoveDuplicates(ref List<string> list_lines, ref int Duplicate_Count)
{
    char[] splitter = { '|' };
    list_lines.ForEach(delegate(string line)
    {
        // ??
    });
}

编辑:
该列表中一些重复的电子邮件地址有不同的部分 ->
我能对他们做些什么:
意思是

goodzila@yahoo.com|32432|test23   
and   
goodzila@yahoo.com|asdsa|324234

提前致谢。

【问题讨论】:

  • 不重复 - 我的 q 不同/请看我的评论...
  • 为什么天哪,你有“无 LINQ”作为要求?
  • @Steven 寻找和学习可能的方法...
  • 其他问题中的许多回答都可以解决您的问题。

标签: c# .net list duplicates


【解决方案1】:

假设您有一个可能重复的列表:

List<string> emailList ....

那么唯一列表就是那个列表的集合:

HashSet<string> unique = new HashSet<string>( emailList )

【讨论】:

  • 感谢您的回答/但该列表中的一些重复的电子邮件地址有不同的部分 -> 我可以对它们做些什么(意思是:goodzila@yahoo.com|32432|test23 和 goodzila@yahoo.com| asdsa|324234)
【解决方案2】:

最简单的做法是遍历文件中的行并将它们添加到 HashSet。 HashSets 不会插入重复的条目,也不会产生异常。最后,您将拥有一个唯一的项目列表,并且不会为任何重复项生成异常。

【讨论】:

    【解决方案3】:
    private void RemoveDuplicates(ref List<string> list_lines, ref int Duplicate_Count)
    {
        Duplicate_Count = 0;
        List<string> list_lines2 = new List<string>();
        HashSet<string> hash = new HashSet<string>();
    
        foreach (string line in list_lines)
        {
            string[] split = line.Split('|');
            string firstPart = split.Length > 0 ? split[0] : string.Empty;
    
            if (hash.Add(firstPart)) 
            {
                list_lines2.Add(line);
            }
            else
            {
                Duplicate_Count++;
            }
        }
    
        list_lines = list_lines2;
    }
    

    【讨论】:

      【解决方案4】:

      1 - 去掉你的管道分隔字符串(创建一个与它所代表的数据相对应的 dto 类)

      2 - 你想应用哪个规则来选择两个具有相同 id 的对象?

      【讨论】:

        【解决方案5】:

        或者也许这段代码对你有用:) 它使用与@xanatos answer中的方法相同的方法

        string[] lines= File.ReadAllLines(filePath);
        Dictionary<string, string> items;
        
        foreach (var line in lines )
        {
            var key = line.Split('|').ElementAt(0);
            if (!items.ContainsKey(key))
                items.Add(key, line);
        }
        List<string> list_lines = items.Values.ToList();
        

        【讨论】:

          【解决方案6】:

          首先,我建议您通过流加载文件。 然后,创建一个代表您的行的类型并将它们加载到 HashSet(for 性能考虑)。

          看(为了简单起见,我删除了你的一些代码):

          public struct LineType
          {
              public string Email { get; set; }
              public string Others { get; set; }
          
              public override bool Equals(object obj)
              {
                  return this.Email.Equals(((LineType)obj).Email);
              }
          }
          private static void WorkOnFile(string filePath)
          {
              StreamReader stream = File.OpenText(filePath);
          
              HashSet<LineType> hashSet = new HashSet<LineType>();
          
              while (true)
              {
                  string line = stream.ReadLine();
                  if (line == null)
                      break;
          
                  string new_line = string.Join("|", line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries));
          
          
                  LineType lineType = new LineType()
                  {
                      Email = new_line.Split('|')[3],
                      Others = new_line
                  };
          
                  if (!hashSet.Contains(lineType))
                      hashSet.Add(lineType);
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-11-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-04-23
            相关资源
            最近更新 更多