Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays C#LINQ方法确定数组是否是另一个数组(包括重复数组)的子集?_Arrays_Linq - Fatal编程技术网

Arrays C#LINQ方法确定数组是否是另一个数组(包括重复数组)的子集?

Arrays C#LINQ方法确定数组是否是另一个数组(包括重复数组)的子集?,arrays,linq,Arrays,Linq,考虑两个阵列: int[] a1 = new int[] { 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10 }; int[] a2 = new int[] { 1, 3, 4, 7, 5, 10, 1 }; 考虑到重复项目的数量,我希望能够确定a2是否是a1的子集。 换句话说,如果a1有三个“1”,a2有四个“1”,那么a2不是a1的子集。然而,如果a1包含三个“1”,那么只要a2包含三个或更少的“1”,它就应该被视为一个子集 使用LINQ的“Intersec

考虑两个阵列:

int[] a1 = new int[] { 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10 };
int[] a2 = new int[] { 1, 3, 4, 7, 5, 10, 1 };
考虑到重复项目的数量,我希望能够确定a2是否是a1的子集。

换句话说,如果a1有三个“1”,a2有四个“1”,那么a2不是a1的子集。然而,如果a1包含三个“1”,那么只要a2包含三个或更少的“1”,它就应该被视为一个子集

使用LINQ的“Intersect”语句的方法不起作用,因为在两个数组(每个数组包含三个“1”)上执行Intersect操作会返回一个只有一个“1”的数组。无论两个数组中有多少个“1”,它都会这样做;它只是查看两个数组中是否都存在该项

示例(基于上面的数组a1):

有没有一种方法可以使用LINQ实现期望的结果

我考虑过一种相当丑陋的方法,将所有数组转换为列表、迭代并从列表中删除项。我只是好奇是否有更优雅的解决方案。

类程序
class Program
    {
        static void Main(string[] args)
        {
            int[] a1 = new int[] { 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10 };
            int[] a2 = new int[] { 1, 3, 4, 7, 5, 10, 1 };
            int[] a3 = new int[] { 1 };                      // True
            int[] a4 = new int[] { 9, 3, 5, 1, 1, 10, 10 };  // True
            int[] a5 = new int[] { 1, 1, 1, 1, 10, 10 };     // False
            int[] a6 = new int[] { 1, 2, 3, 3, 4, 5 };       // False
            int[] a7 = new int[] { 10, 10, 10 };             // False
            int[] a8 = new int[0];

            Console.WriteLine(a2.IsSubSetOf(a1));
            Console.WriteLine(a3.IsSubSetOf(a1));
            Console.WriteLine(a4.IsSubSetOf(a1));
            Console.WriteLine(a5.IsSubSetOf(a1));
            Console.WriteLine(a6.IsSubSetOf(a1));
            Console.WriteLine(a7.IsSubSetOf(a1));
            Console.WriteLine(a8.IsSubSetOf(a1));
            Console.ReadLine();
        }
    }

    public static class Ext
    {
        public static bool IsSubSetOf(this IEnumerable<int> other, IEnumerable<int> a1)
        {
            var a1Group = a1.GroupBy(i => i).Select(g => new { Num = g.Key, Count = g.Count() }).ToList();
            var otherGroup = other.GroupBy(i => i).Select(g => new { Num = g.Key, Count = g.Count() }).ToList();

            return other != null
                   && other.Any()
                   && otherGroup.Count <= a1Group.Count
                   && otherGroup.All(o => a1Group.Single(a => a.Num == o.Num).Count >= o.Count);
        }
    }
{ 静态void Main(字符串[]参数) { int[]a1=新的int[]{1,1,1,2,3,4,5,6,7,8,9,10}; int[]a2=新的int[]{1,3,4,7,5,10,1}; int[]a3=新的int[]{1};//True int[]a4=新的int[]{9,3,5,1,1,10,10};//True int[]a5=新的int[]{1,1,1,1,10,10};//False int[]a6=新的int[]{1,2,3,3,4,5};//False int[]a7=新的int[]{10,10,10};//False int[]a8=新的int[0]; 控制台。写入线(a2。发布日期(a1)); 控制台。写入线(a3。发布日期(a1)); 控制台。写入线(a4。发布日期(a1)); 控制台。写入线(a5。问题(a1)); 控制台。写入线(a6。问题(a1)); 控制台。写入线(a7。问题(a1)); 控制台。写入线(a8。问题(a1)); Console.ReadLine(); } } 公共静态类Ext { 公共静态bool issuetof(此IEnumerable其他,IEnumerable a1) { var a1Group=a1.GroupBy(i=>i).Select(g=>new{Num=g.Key,Count=g.Count()}).ToList(); var otherGroup=other.GroupBy(i=>i).Select(g=>new{Num=g.Key,Count=g.Count()}).ToList(); 返回其他!=null &&其他 &&otherGroup.counta1group.Single(a=>a.Num==o.Num.Count>=o.Count); } }
这是一行。可读性值得怀疑。它还需要一个附加子句来处理空数组

bool isSubset = ints.GroupBy(i => i).All(g => a1.Count(i => i == g.Key) >= g.Count()) && ints.Count() > 0;

这对我来说很直接:

var l1 = a1.ToLookup(x => x);
var l2 = a2.ToLookup(x => x);

var check = l2.All(xs => xs.Count() <= l1[xs.Key].Count());
var l1=a1.ToLookup(x=>x);
VarL2=a2.ToLookup(x=>x);
var check=l2.All(xs=>xs.Count()
var l1 = a1.ToLookup(x => x);
var l2 = a2.ToLookup(x => x);

var check = l2.All(xs => xs.Count() <= l1[xs.Key].Count());