Syn*_*ter 5 c# visual-studio-2005 duplicate-removal .net-2.0
我想知道一种从C#中的字符串数组中删除重复项的有效方法.
例如,
string[] a = { "abc", "xyz","abc", "def", "ghi", "asdf", "ghi","xd", "abc" };
Run Code Online (Sandbox Code Playgroud)
会变成,
string[] a = { "abc", "xyz","def", "ghi", "asdf", "xd" };
Run Code Online (Sandbox Code Playgroud)
删除重复条目后如何填补空白?有没有办法在不使用额外数组存储元素的情况下执行此操作?
我使用的方法:
1) Sorted the array
2) Replaced the duplicate entries with null
3) Copied NOT null string to a new array.
Run Code Online (Sandbox Code Playgroud)
但寻找一种优化的方法来做同样的事情.
编辑:我使用的是.NET 2.0和VS 2005
Oha*_*der 12
您可以使用HashSet:
string[] a = { "abc", "xyz","abc", "def", "ghi", "asdf", "ghi","xd", "abc" };
var b = new HashSet<string>(a);
Run Code Online (Sandbox Code Playgroud)
您无法在.NET中调整数组大小,因此无论您使用何种方法删除重复项,都必须为结果创建一个新数组.
您可以使用a HashSet<string>轻松删除重复项:
a = new HashSet<string>(a).ToArray();
Run Code Online (Sandbox Code Playgroud)
哈希集将数组中的项添加到自身,并自动丢弃重复项.由于哈希集使用哈希码来检查现有项目,这比排序项目要快一些,但结果当然没有排序.
如果使用.NET 3.0,您可以使用LINQ:
using System;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] a = { "abc", "xyz", "abc", "def", "ghi", "asdf", "ghi", "xd", "abc" };
string[] b = a.Distinct().ToArray();
foreach (string s in b)
Console.WriteLine(s);
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)