gpt4 book ai didi

c# - 比较元组,忽略元素的顺序

转载 作者:太空宇宙 更新时间:2023-11-03 21:01:56 24 4
gpt4 key购买 nike

假设我有对象 Foo 和 Bar;和名为 A、B 和 C 的 3 个元组。

A = (Foo, Bar)
B = (Bar, Foo)
C = (Foo, Bar)

我想知道它们的元素是否相同,而不考虑元素的顺序。所以我想要的结果是;

A.HasSameElementsWith(B)  -> True
A.HasSameElementsWith(C) -> True
B.HasSameElementsWith(C) -> True

我知道我可以运行一个嵌套循环来比较它们的每个元素。类似的东西:

foreach (itemA in A)
{
bool flag = false;

foreach (itemB in B)
{
if(itemA == itemB)
{
flag = true;
break;
}
}

if (!flag) return false;
}

return true;

但这似乎效率低下。有没有更方便的方法来做到这一点?


注意:

我正在使用泛型,所以 FooBar 可以是任何类型。但它们彼此将是同一类型。 (即 Foo 的类型与 Bar 的类型相同)

最佳答案

如果您有两个二元组,那么根据您的规则只有两个选项可以让它们相等,您可以将其写成几乎是单行方法:

public static bool HasSameElementsWith<T>(this (T, T) tuple1, (T, T) tuple2) =>
(Equals(tuple1.Item1, tuple2.Item1) && Equals(tuple1.Item2, tuple2.Item2)) ||
(Equals(tuple1.Item1, tuple2.Item2) && Equals(tuple1.Item2, tuple2.Item1));

如果每个元组可以有两个以上的项目,那么我会开始将它们视为一个集合,然后问题就变成了两个集合是否具有相同的项目。为此,您可以计算 Dictionary<T, int> 中第一个集合中的每个项目。 ,然后倒数第二个集合中的项目。如果两个集合包含相同的项目,则最后所有计数都应为零。 (如果您确定每个集合中的项目都是唯一的,则可以改用 HashSet<T>。)在代码中:

public static bool HasSameElementsWith<T>(
this IEnumerable<T> collection1, IEnumerable<T> collection2)
{
var counts = new Dictionary<T, int>();

foreach (var item in collection1)
{
counts.TryGetValue(item, out int count);
count++;
counts[item] = count;
}

foreach (var item in collection2)
{
counts.TryGetValue(item, out int count);
if (count == 0)
return false;
count--;
counts[item] = count;
}

return counts.Values.All(c => c == 0);
}

现在你可以实现 HasSameElementsWith 的元组版本了在收藏版之上:

public static bool HasSameElementsWith<T>(this (T, T) tuple1, (T, T) tuple2) =>
HasSameElementsWith(tuple1.ToArray(), tuple2.ToArray());

public static T[] ToArray<T>(this (T, T) tuple) => new[] { tuple.Item1, tuple.Item2 };

关于c# - 比较元组,忽略元素的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44698041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com