所以这里是我的数组。如何检查我的数组内部是否有重复值?
double[] testArray = new double[10];
// will generate a random numbers from 1-20, too lazy to write the code
我想做一个搜索循环来检查是否有任何值正在重复。我怎么做?
我不想使用任何特殊的内置方法,因为这是一个小阵列。
所以这里是我的数组。如何检查我的数组内部是否有重复值?
double[] testArray = new double[10];
// will generate a random numbers from 1-20, too lazy to write the code
我想做一个搜索循环来检查是否有任何值正在重复。我怎么做?
我不想使用任何特殊的内置方法,因为这是一个小阵列。
你可以用少许的Linq做到这一点:
if (testArray.Length != testArray.Distinct().Count())
{
Console.WriteLine("Contains duplicates");
}
的Distinct
扩展方法删除任何重复,Count
得到结果集的大小。如果它们完全不同,那么列表中有一些重复项。
可替代地,这里的更复杂的查询,但也可以是更多的有效的:
if (testArray.GroupBy(x => x).Any(g => g.Count() > 1))
{
Console.WriteLine("Contains duplicates");
}
的GroupBy
方法将基团的任何相同的元件一起,和Any
返回true
如果任何组中有一个以上的元件。
上述两种解决方案通过利用HashSet<T>
工作,但你可以直接使用一个这样的:
if (!testArray.All(new HashSet<double>().Add))
{
Console.WriteLine("Contains duplicates");
}
或者如果你喜欢,不依赖于LINQ的在所有的解决方案:
var hashSet = new HashSet<double>();
foreach(var x in testArray)
{
if (!hashSet.Add(x))
{
Console.WriteLine("Contains duplicates");
break;
}
}
使用此:
bool CheckUniqueness(double[] values)
{
var uniqueValues = new HashSet<double>();
foreach (double d in values)
{
if(uniqueValues.Contains(d))
{
return false;
}
uniqueValues.Add(d);
}
return true;
}
带(OP)10个随机双打相当快。 重复的机会:〜0.000002%。
static bool repeat(double[] a)
{
return
a[0] == a[1] || a[0] == a[2] || a[0] == a[3] || a[0] == a[4] ||
a[0] == a[5] || a[0] == a[6] || a[0] == a[7] || a[0] == a[8] ||
a[0] == a[9] || a[1] == a[2] || a[1] == a[3] || a[1] == a[4] ||
a[1] == a[5] || a[1] == a[6] || a[1] == a[7] || a[1] == a[8] ||
a[1] == a[9] || a[2] == a[3] || a[2] == a[4] || a[2] == a[5] ||
a[2] == a[6] || a[2] == a[7] || a[2] == a[8] || a[2] == a[9] ||
a[3] == a[4] || a[3] == a[5] || a[3] == a[6] || a[3] == a[7] ||
a[3] == a[8] || a[3] == a[9] || a[4] == a[5] || a[4] == a[6] ||
a[4] == a[7] || a[4] == a[8] || a[4] == a[9] || a[5] == a[6] ||
a[5] == a[7] || a[5] == a[8] || a[5] == a[9] || a[6] == a[7] ||
a[6] == a[8] || a[6] == a[9] || a[7] == a[8] || a[7] == a[9] ||
a[8] == a[9];
}
更一般情况下,用10号比上面慢〜2倍,比HashSet的方法快
但〜7倍。
static bool repeat(double[] a)
{
int k = a.Length - 1;
if (k < 70)
{
double aj;
for (int i = 0, j; i < k;)
{
for (aj = a[k--], j = k; j >= i; j--)
if (aj == a[j]) return true;
for (aj = a[i++], j = i; j <= k; j++)
if (aj == a[j]) return true;
}
return false;
}
var h = new HashSet<double>();
while (k >= 0) if (!h.Add(a[k--])) return false;
return true;
}
两行(有重复慢;)
static bool repeat(double[] a)
{ return (new HashSet<double>(a).Count < a.Length); }
采取看看我的实现其generic
和efficient
public static bool HasDuplicates<T>(IList<T> items)
{
Dictionary<T, bool> map = new Dictionary<T, bool>();
for (int i = 0; i < items.Count; i++)
{
if (map.ContainsKey(items[i]))
{
return true; // has duplicates
}
map.Add(items[i], true);
}
return false; // no duplicates
}
这里有一些电话
string[] strings = new[] { "1", "2", "3" };
Utility.HasDuplicates(strings)// this will return false
int[] items=new []{1,2,3,1};
Utility.HasDuplicates(items)// this will return true
哦好,谢谢。 Distinct()。Count()应该做的一般是什么? – puretppc
@Mike查看我的更新回答 –
哦,我明白了。所以如果我只想检查重复项,那么我只是删除Distinct()的权利? Count()方法意味着它有多少次与结果集的大小一样重复? – puretppc