1

In C#, given an array of integers that represents indexes of an array of items, is there a way to get a sub-array of the array of items that correspond to indexes in one step?

int[] indexesArray = {0,2,4,1};
string[] itemsArray = {"hi", "ciao", "yo"," hey","hello"};

string[] result = builtinMagic(itemsArray, indexesArray);
Patrick Hofman
  • 148,824
  • 21
  • 237
  • 306
D.Giunchi
  • 1,860
  • 2
  • 19
  • 22

1 Answers1

5

You can simply Select the index from the indexesArray and then get the item at that specific index:

string[] result = indexesArray.Select(idx => itemsArray[idx]).ToArray();
Patrick Hofman
  • 148,824
  • 21
  • 237
  • 306