Lets assume we have two string arrays
string[] array1 = {"aa", "bb", "cc"};
string[] array2 = {"bb", "aa", "cc", "bb", "aa", "cc"};
now the string bb in array2 is in second position of array1 so the output should be 2
aa in array2 is in first position of array1 so the output should be 1
cc in array2 is in third position of array1 so the output should be 3
bb in array2 is in second position of array1 so the output should be 2
so on..
all these output index positions should be stored in an array..
any idea how to accomplish this in C#.net?
Something like this:
This will project the elements of
array2as a sequence of their indicies inarray1, and convert the result to an array.As pointed out in the comments, the original requirement was that “aa” map to the value 1, even though its index in array1 is 0. Offsetting the computed index by 1 will fix this:
With this modification an element not found in array1 will result in an index of 0 instead of -1, of course.