-1

I have a foreach that loops over two arrays to check if the numbers are matching and in the same order as each other.

However one of the arrays can have some extra numbers that I want to remove/ignore before comparing.

Example:

int[] currentTracingPoints = new int[] { 1,2,3,4 };
int[] part = new int[] {1,2,3};

I would like to remove the number 4 from currentTracingPoints

Example:

int[] currentTracingPoints = new int[] {4,5,6,8};
int[] part = new int[] {4,5,8};

I would like to remove the number 6 from currentTracingPoints

I am using Unity

foreach (TracingPart part in tracingParts) { // check tracing parts
    if (currentTracingPoints.Count == part.order.Length && !part.succeded) {
        // check whether the previous tracing parts are succeeded
        if (PreviousLettersPartsSucceeded (part, tracingParts)) {
            equivfound = true; // assume true
            for (int i = 0; i < currentTracingPoints.Count; i++) {
                int index = (int) currentTracingPoints [i];
                if (index != part.order [i]) {
                    equivfound = false;
                    break;
                }
            }
        }
    }
}
theduck
  • 2,589
  • 13
  • 17
  • 23
Keith Power
  • 13,891
  • 22
  • 66
  • 135
  • ...and what is your question? What is not working right? – frankhermes Sep 04 '19 at 18:57
  • In both cases the result you want in `currentTracingPoints` seems to be just `part`. Why not just use `part`? What am I missing? – yes Sep 04 '19 at 19:00
  • currently the code works if both `arrays` are exactly the same values in the same order `{1,2,3}` . However sometimes `currentTracingPoints` will have have extra numbers that I want to remove first before I compare check to see if both `arrays` have the same order of numbers. – Keith Power Sep 04 '19 at 19:02
  • The two `arrays` that I compare are `currentTracingPoints` and `part`. Everything else are just Unity parent `Objects` – Keith Power Sep 04 '19 at 19:05
  • Really you could ignore the the `foreach` code. i just put it there as context for what I am doing. Really I just want to look at two `arrays` and remove the extra values form `currentTracingPoints`. Then I can do my compare checking after – Keith Power Sep 04 '19 at 19:07
  • If you want to dynamically remove elements you would be better using `List` .. and then see [this answer](https://stackoverflow.com/a/54677133/7111561) which provides a **match between lists that can be scattered** `var result = B.Intersect(A).SequenceEqual(B)` – derHugo Sep 04 '19 at 20:24
  • and what if arrays have same values but in different order, say currentTracingPoints ={1,2,3} and part = {1,3,2}. what would be the desired resultant array? – Rashid Ali Sep 04 '19 at 22:06

3 Answers3

0

You could get the difference between the two arrays by using LINQ:

int[] diff = currentTracingPoints.Except(part);

And then when iterating in your loop, you can see if the current part exists in the diff array, and if it does just skip the current iteration.

Hopefully I am understanding what you need correctly, if not please let me know.

deruitda
  • 580
  • 4
  • 17
0

You can use an Intersect, using Linq:

var result = currentTracingPoints.Intersect(part);

This one compares and remove the elements that are not in both arrays.

It's the equivalent of the mathematical intersection.


int[] currentTracingPoints = new int[] { 1,2,3,4 };
int[] part = new int[] {1,2,3};

currentTracingPoints.Intersect(part);

foreach(var x in currentTracingPoints.Intersect(part))
    Console.WriteLine(x);

output:

1
2
3
Stefan
  • 17,448
  • 11
  • 60
  • 79
0

If you are interested in removing the elements with your own code. I would use lists for each array because they would be more malleable.

You would iterate over currentTracingPoints backwards, and remove any that can't be found in the part array.

List< int > currentTracingPoints = new List<int> ( { 1, 2, 3, 4 } );
List< int > parts = new List<int> ( { 1, 2, 3 } );

for ( int index = currentTracingPoints.Count-1; index >= 0; index-- )
{
    if ( !parts.Contains( currentTracingPoints[ index ] ) )
    {
        currentTracingPoints.RemoveAt( index );
    }
}

And then you will be left an array that matches parts. Then you can convert the list to an int array with ToArray

I'm not 100% sure if this absolutely the fastest, but it would work. Wether or not it becomes a performance bottleneck would depend on the size of the arrays and other nessary processes surrounding it.