C#实现List<T>去重的多种高效方法

 更新时间:2025年12月10日 09:09:32   作者:凌霜残雪  
在 C# 开发中,处理集合数据是日常开发任务的重要组成部分,有时我们需要对 List<T> 进行去重操作,以确保集合中的元素唯一,本文将介绍几种实现 List<T> 去重的方法,并探讨它们的优缺点和适用场景,需要的朋友可以参考下

引言

在 C# 开发中,处理集合数据是日常开发任务的重要组成部分。有时我们需要对 List<T> 进行去重操作,以确保集合中的元素唯一。本文将介绍几种实现 List<T> 去重的方法,并探讨它们的优缺点和适用场景。C# 提供了多种方式来完成这项任务,每种方法都有其独特的优势。接下来,让我们深入探讨这些方法吧!

方法一:使用 HashSet

HashSet<T> 是一个不允许重复元素的集合类型,它基于哈希表实现,因此提供了非常快的查找速度。

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        List<int> listA = new List<int> { 1, 2, 3, 4, 2, 3, 5 };
        
        // 使用 HashSet 去重
        HashSet<int> hashSet = new HashSet<int>(listA);
        List<int> uniqueList = new List<int>(hashSet);

        Console.WriteLine(string.Join(", ", uniqueList)); // 输出:1, 2, 3, 4, 5
    }
}

优点

  • 高效:时间复杂度为 O(n)。
  • 简洁:代码量少,易于理解。

方法二:LINQ 的 Distinct 方法

如果你已经习惯了 LINQ 的强大功能,那么 Distinct() 方法无疑是你的首选。它可以直接应用于任何实现了 IEnumerable<T> 接口的集合。

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        List<int> listA = new List<int> { 1, 2, 3, 4, 2, 3, 5 };

        // 使用 LINQ 的 Distinct 方法去重
        List<int> uniqueList = listA.Distinct().ToList();

        Console.WriteLine(string.Join(", ", uniqueList)); // 输出:1, 2, 3, 4, 5
    }
}

优点

  • 链式调用友好:可以与其他 LINQ 操作无缝集成。
  • 简单直观:一行代码即可完成去重。

方法三:手动去重(循环 + 判断)

对于那些希望完全控制去重逻辑的开发者来说,手动遍历并判断是否已存在当前元素是一种传统但有效的手段。

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        List<int> listA = new List<int> { 1, 2, 3, 4, 2, 3, 5 };
        List<int> uniqueList = new List<int>();

        foreach (int item in listA)
        {
            if (!uniqueList.Contains(item))
            {
                uniqueList.Add(item);
            }
        }

        Console.WriteLine(string.Join(", ", uniqueList)); // 输出:1, 2, 3, 4, 5
    }
}

优点

  • 灵活:可以根据需求调整去重逻辑。
  • 不依赖外部工具:完全手写逻辑,适合学习基础。

缺点

  • 性能较差:尤其是当列表较大时,Contains 的时间复杂度为 O(n),整体效率较低。

方法四:使用 Dictionary 或 Lookup

Dictionary<K,V>Lookup<K,V> 也可以用来去重,因为它们的键是唯一的。

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        List<int> listA = new List<int> { 1, 2, 3, 4, 2, 3, 5 };

        // 使用 Dictionary 去重
        Dictionary<int, bool> dict = new Dictionary<int, bool>();
        foreach (int item in listA)
        {
            if (!dict.ContainsKey(item))
            {
                dict[item] = true;
            }
        }

        List<int> uniqueList = new List<int>(dict.Keys);

        Console.WriteLine(string.Join(", ", uniqueList)); // 输出:1, 2, 3, 4, 5
    }
}

优点

  • 性能较好:查找速度快,适用于大数据集。
  • 键值结构灵活:适合扩展。

方法五:排序后去重

如果允许改变原始列表顺序,可以通过排序后移除相邻重复项来去重。

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        List<int> listA = new List<int> { 1, 2, 3, 4, 2, 3, 5 };

        // 排序后去重
        listA.Sort();
        List<int> uniqueList = new List<int> { listA[0] };

        for (int i = 1; i < listA.Count; i++)
        {
            if (listA[i] != listA[i - 1])
            {
                uniqueList.Add(listA[i]);
            }
        }

        Console.WriteLine(string.Join(", ", uniqueList)); // 输出:1, 2, 3, 4, 5
    }
}

优点

  • 适合排序场景:先排序再去除重复项。
  • 逻辑简单:直接比较相邻元素。

缺点

  • 会改变原始顺序:不适合对顺序敏感的情况。

结语

选择哪种去重方法取决于具体的应用场景和个人偏好。无论你是追求极致性能还是代码简洁性,总有一种方法能满足你的需求。希望这篇文章能帮助你更好地理解和应用 C# 中的 List<T> 去重技术!

通过这篇博客文章,不仅可以让读者了解到不同的去重方法,还能帮助他们在实际工作中根据具体情况选择最合适的解决方案。

以上就是C#实现List<T>去重的多种高效方法的详细内容,更多关于C# List<T>去重的资料请关注脚本之家其它相关文章!

相关文章

最新评论