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
  • 153,850
  • 22
  • 249
  • 325
D.Giunchi
  • 1,900
  • 3
  • 19
  • 23

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
  • 153,850
  • 22
  • 249
  • 325