Home > other >  Can we define value from confirmed duplicates in arrays & how C#
Can we define value from confirmed duplicates in arrays & how C#

Time:04-28

Can we define value from confirmed duplicates in 3 4 5 arrays & how? Probably this is wrong path I take for finding duplicates, but better to ask than to roam all over my small brain and eventualy delete this and get back to array matrix... I need to extract - define that int value that duplicates and later how much time in each of the 5 rows it exists. So to summarize: How to get that duplicated value "int" so I can use it for later checks.

public void Check() 

{
    var result1 = Row1.Any(L1 => Row2.Contains(L1) && Row3.Contains(L1)) == true;

    var result2 = Row1.Any(L1 => Row2.Contains(L1) && Row3.Contains(L1) && Row4.Contains(L1)) == true;

    var result3 = Row1.Any(L1 => Row2.Contains(L1) && Row3.Contains(L1) && Row4.Contains(L1) && Row5.Contains(L1)) == true;

        if(result3 == true) { result1 = false; result2 = false; Debug.Log("Line 5"); }

        if(result2 == true) { result1 = false; Debug.Log("Line 4"); }

        if(result1 == true) { Debug.Log("Line 3"); }
}

CodePudding user response:

If I'm reading your question correctly, you simply want the Intersect extension method, e.g.

int[] id1 = { 44, 26, 92, 30, 71, 38 };
int[] id2 = { 39, 59, 83, 47, 26, 4, 30 };

IEnumerable<int> both = id1.Intersect(id2);

foreach (int id in both)
    Console.WriteLine(id);

/*
 This code produces the following output:

 26
 30
*/

CodePudding user response:

If the goal is to find the intersection, i.e. all items that are present in one or more arrays, then you can use the linq Intersect function.

var allItemsThatAreInBothRow1AndRow2 = Row1.Intersect(Row2);

And it can be chained

var allItemsInRow1234= Row1
                .Intersect(Row2)
                .Intersect(Row3);
                .Intersect(Row4);
  • Related