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);
2
  • This is called slicing Commented Jun 14, 2017 at 10:37
  • Use a 2D/ multidimensional array? Commented Jun 14, 2017 at 10:37

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

You are too fast for me :)
I have that a lot ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.