从 C# 中的字符串数组中删除重复字符串的有效方法 [英] Efficient way to remove duplicate strings from a string array in C#

查看:39
本文介绍了从 C# 中的字符串数组中删除重复字符串的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一种从 C# 中的字符串数组中删除重复项的有效方法.

I would like to know an efficient method to remove duplicate items from a string array in C#.

例如

string[] a = { "abc", "xyz","abc", "def", "ghi", "asdf", "ghi","xd", "abc" };

会变成,

string[] a = { "abc", "xyz","def", "ghi", "asdf", "xd" };

删除重复条目后如何填补空白?有没有办法在不使用额外数组来存储元素的情况下做到这一点?

How to fill the gaps after removing the duplicate entries? Is there a way to do this without using an extra array for storing the elements?

我使用的方法:

1) Sorted the array

2) Replaced the duplicate entries with null

3) Copied NOT null string to a new array.

但正在寻找一种优化的方法来做同样的事情.

But looking for an optimized way to doing the same.

我使用 .NET 2.0 和 VS 2005

I am using .NET 2.0 and VS 2005

推荐答案

您可以使用 HashSet:

You can use a HashSet:

string[] a = { "abc", "xyz","abc", "def", "ghi", "asdf", "ghi","xd", "abc" };
var b = new HashSet<string>(a);

这篇关于从 C# 中的字符串数组中删除重复字符串的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆