鉴于有2个阵列,例如foo
和bar
,检查阵列条至少含有1个 f物品的最有效方式。 应当恢复真实/真实。
我怀疑嵌套的 foreach
,但我想知道是否有更好的方法?
鉴于有2个阵列,例如foo
和bar
,检查阵列条至少含有1个 f物品的最有效方式。 应当恢复真实/真实。
我怀疑嵌套的 foreach
,但我想知道是否有更好的方法?
使用准则:
array1.Intersect(array2).Any()
注:使用<代码>Any(),保证在发现第一个平等对象时,交叉算法即告停止。
C#3:
bool result = bar.Any(el => foo.Contains(el));
C#4平行执行:
bool result = bar.AsParallel().Any(el => foo.AsParallel().Contains(el));
是的,虽然其中一个循环被隐藏了,但仍存在嵌套循环。
bool AnyAny(int[] A, int[]B)
{
foreach(int i in A)
if (B.Any(b=> b == i))
return true;
return false;
}
另一个解决方案:
var result = array1.Any(l2 => array2.Contains(l2)) == true ? "its there": "not there";
如果您使用的是类而不是内置数据类型(如 int 等),则需要重写您自己的类的 Equals 和 GetHashCode 实现。
独特性是可选的,取决于您的第一个数组(是否具有唯一值)...
String[] A = new String[] {"2","1","3","2"};
String[] B = new String[] {"1","2", "3", "3", "1", "1", "2"};
Console.WriteLine("A values: "+String.Join(", ",A));
Console.WriteLine("B values: "+String.Join(", ",B));
Console.WriteLine("Comparison A and B result: "+ A.Distinct().Intersect(B).SequenceEqual(A.Distinct()));
static void Main(string[] args)
int[] arr1 = { 16, 48, 40, 32, 5, 7 };
int[] arr2 = { 48, 32, 16, 40, 56, 72, 16, 16, 16, 16, 16, 5, 7, 6, 56 };
int k = 0;
for (int i = 0; i < arr1.Length; i++)
{
for (int j = 0; j < arr2.Length; j++)
{
if (arr1[i] == arr2[j])
{
k++;
break;
}
}
}
if (arr1.Length == k)
{
Console.WriteLine(true);
}
else
Console.WriteLine(false);
}
----
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...