Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reversing a integer value list

Tags:

c#

I have a list of integer values which can be anywhere between 1 and 4. So, let's say {1,2,4,1,3,2,1,4,4} for instance. I now want to reverse the values in the following way: All entries with ...

  • 1 should be converted to 4,
  • 2 should be converted to 3,
  • 3 should be converted to 2,
  • 4 should be converted to 1.

There are numerous ways to do this but I want to take the most efficient approach.

Any thoughts?

like image 324
jvb Avatar asked Mar 19 '26 05:03

jvb


2 Answers

for(int i = 0; i < array.Length; i++)
{
    array[i] = 5 - array[i];
}
like image 161
Stecya Avatar answered Mar 21 '26 18:03

Stecya


Implement this function:

f(x) = 5 - x

like image 28
CloudyMarble Avatar answered Mar 21 '26 19:03

CloudyMarble