Some comments
What I would like to get are ids of b list elements in list a.
Nowhere in your question are there IDs mentioned, also not in the data example. I am going to assume you want the indexes (i.e. the position in list a of the matched element.
arrays contains always pair of int
I suggest using a Tuple<int,int>
. Which I will use in a code example, for the sake of easier processing. But I'll make it so that you can start off with your initial arrays, in order to maximize compatibility with your code.
Another issue is that condition is true if pair is 0 1 flipped. So 0;1 and 0;1 is true and 0;1 and 1;0 is true;
The solution therefore is to order each element pair (not the list of element itself), so that we match on integer values regardless of their position.
If you need to preserve the order of the numbers (for other purposes), then you'll have to make a copy of the numbers (for the purpose of this matching algorithm), and only sort that copy.
The actual question
Is there a better way than looping through list a , checking array elements with if statement and if true add those elements?
Not really. You are going to have to match every element A to every element B.
However, if you are talking about large data sets; you might gain a bit of performance by doing some preprocessing (mainly ordering of entries etc.).
It somewhat depends on what you mean with "a better way". Are you trying to:
- Maximize the speed of processing large data sets?
- Improve the readability of the code?
- Shorten the code itself?
Depending on what your focus is, you will need a different approach. And that is not clear from your question as it is currently phrased.