Here is an adaptation of your code that doesn’t require storing intermediate results:
private Int32 GetHighest(Int32 y, out Int32 totalMax)
{
var Set = new Int32[] { 4, 3, 2 };
totalMax = int.MinValue;
int itemMax = int.MinValue;
foreach (var x in Set)
{
int total = x * (y / x);
if(total >= totalMax && (total > totalMax || x > itemMax))
{
totalMax = total;
itemMax = x;
}
}
return itemMax;
}
0
solved Find Highest Divisible Integer In Set By X