Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

This is a "follow up" to an answer I gave on this question : Summing up distinct elements in steps

Here are the OP's requirements :

"My current task is to find a score from an array where the highest/lowest scores have been taken away, and if the highest/lowest occur more than once (ONLY if they occur more than once), one of them can be added:

E.g. int[] scores = [4, 8, 6, 4, 8, 5] therefore the final addition will be ∑4,8,6,5=23.

Another condition of the task is that LINQ cannot be used, as well as any of the System.Array methods (you can see by my previously ask questions that has been a bit of a pain for me, since I solved this with LINQ in less than 5 minutes)."

public int CalculateScore(int[] scores)
{
 int lowestValue = int.MaxValue,
 highestValue = int.MinValue,
 ammountOfHighestValue = 1,
 ammountOfLowestValue = 1,
 finalScore = 0;
 foreach (int score in scores)
 {
 finalScore += score;
 if (score < lowestValue)
 {
 lowestValue = score;
 ammountOfLowestValue = 1; //We need to reset the ammount
 }
 else if (score > highestValue)
 {
 highestValue = score;
 ammountOfHighestValue = 1; //We need to reset the ammount
 }
 else if (score == lowestValue)
 ammountOfLowestValue++;
 else if (score == highestValue)
 ammountOfHighestValue++;
 }
 if (ammountOfHighestValue > 1)
 //This way, we keep the highest score once.
 finalScore -= ((ammountOfHighestValue - 1) * highestValue); 
 else
 finalScore -= highestValue; //The value is there once, we remove it.
 if (ammountOfLowestValue > 1)
 finalScore -= ((ammountOfLowestValue - 1) * lowestValue); //Same as highest
 else
 finalScore -= lowestValue;
 return finalScore;
}

I'm interested about how can I remove the multiple if/else statements (削除) while keeping a complexity of O(n) (削除ここまで) and still loop through the array only once.

This is a "follow up" to an answer I gave on this question : Summing up distinct elements in steps

Here are the OP's requirements :

"My current task is to find a score from an array where the highest/lowest scores have been taken away, and if the highest/lowest occur more than once (ONLY if they occur more than once), one of them can be added:

E.g. int[] scores = [4, 8, 6, 4, 8, 5] therefore the final addition will be ∑4,8,6,5=23.

Another condition of the task is that LINQ cannot be used, as well as any of the System.Array methods (you can see by my previously ask questions that has been a bit of a pain for me, since I solved this with LINQ in less than 5 minutes)."

public int CalculateScore(int[] scores)
{
 int lowestValue = int.MaxValue,
 highestValue = int.MinValue,
 ammountOfHighestValue = 1,
 ammountOfLowestValue = 1,
 finalScore = 0;
 foreach (int score in scores)
 {
 finalScore += score;
 if (score < lowestValue)
 {
 lowestValue = score;
 ammountOfLowestValue = 1; //We need to reset the ammount
 }
 else if (score > highestValue)
 {
 highestValue = score;
 ammountOfHighestValue = 1; //We need to reset the ammount
 }
 else if (score == lowestValue)
 ammountOfLowestValue++;
 else if (score == highestValue)
 ammountOfHighestValue++;
 }
 if (ammountOfHighestValue > 1)
 //This way, we keep the highest score once.
 finalScore -= ((ammountOfHighestValue - 1) * highestValue); 
 else
 finalScore -= highestValue; //The value is there once, we remove it.
 if (ammountOfLowestValue > 1)
 finalScore -= ((ammountOfLowestValue - 1) * lowestValue); //Same as highest
 else
 finalScore -= lowestValue;
 return finalScore;
}

I'm interested about how can I remove the multiple if/else statements (削除) while keeping a complexity of O(n) (削除ここまで) and still loop through the array only once.

This is a "follow up" to an answer I gave on this question : Summing up distinct elements in steps

Here are the OP's requirements :

"My current task is to find a score from an array where the highest/lowest scores have been taken away, and if the highest/lowest occur more than once (ONLY if they occur more than once), one of them can be added:

E.g. int[] scores = [4, 8, 6, 4, 8, 5] therefore the final addition will be ∑4,8,6,5=23.

Another condition of the task is that LINQ cannot be used, as well as any of the System.Array methods (you can see by my previously ask questions that has been a bit of a pain for me, since I solved this with LINQ in less than 5 minutes)."

public int CalculateScore(int[] scores)
{
 int lowestValue = int.MaxValue,
 highestValue = int.MinValue,
 ammountOfHighestValue = 1,
 ammountOfLowestValue = 1,
 finalScore = 0;
 foreach (int score in scores)
 {
 finalScore += score;
 if (score < lowestValue)
 {
 lowestValue = score;
 ammountOfLowestValue = 1; //We need to reset the ammount
 }
 else if (score > highestValue)
 {
 highestValue = score;
 ammountOfHighestValue = 1; //We need to reset the ammount
 }
 else if (score == lowestValue)
 ammountOfLowestValue++;
 else if (score == highestValue)
 ammountOfHighestValue++;
 }
 if (ammountOfHighestValue > 1)
 //This way, we keep the highest score once.
 finalScore -= ((ammountOfHighestValue - 1) * highestValue); 
 else
 finalScore -= highestValue; //The value is there once, we remove it.
 if (ammountOfLowestValue > 1)
 finalScore -= ((ammountOfLowestValue - 1) * lowestValue); //Same as highest
 else
 finalScore -= lowestValue;
 return finalScore;
}

I'm interested about how can I remove the multiple if/else statements (削除) while keeping a complexity of O(n) (削除ここまで) and still loop through the array only once.

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

This is a "follow up" to an answer I gave on this question : Summing up distinct elements in steps Summing up distinct elements in steps

Here are the OP's requirements :

"My current task is to find a score from an array where the highest/lowest scores have been taken away, and if the highest/lowest occur more than once (ONLY if they occur more than once), one of them can be added:

E.g. int[] scores = [4, 8, 6, 4, 8, 5] therefore the final addition will be ∑4,8,6,5=23.

Another condition of the task is that LINQ cannot be used, as well as any of the System.Array methods (you can see by my previously ask questions that has been a bit of a pain for me, since I solved this with LINQ in less than 5 minutes)."

public int CalculateScore(int[] scores)
{
 int lowestValue = int.MaxValue,
 highestValue = int.MinValue,
 ammountOfHighestValue = 1,
 ammountOfLowestValue = 1,
 finalScore = 0;
 foreach (int score in scores)
 {
 finalScore += score;
 if (score < lowestValue)
 {
 lowestValue = score;
 ammountOfLowestValue = 1; //We need to reset the ammount
 }
 else if (score > highestValue)
 {
 highestValue = score;
 ammountOfHighestValue = 1; //We need to reset the ammount
 }
 else if (score == lowestValue)
 ammountOfLowestValue++;
 else if (score == highestValue)
 ammountOfHighestValue++;
 }
 if (ammountOfHighestValue > 1)
 //This way, we keep the highest score once.
 finalScore -= ((ammountOfHighestValue - 1) * highestValue); 
 else
 finalScore -= highestValue; //The value is there once, we remove it.
 if (ammountOfLowestValue > 1)
 finalScore -= ((ammountOfLowestValue - 1) * lowestValue); //Same as highest
 else
 finalScore -= lowestValue;
 return finalScore;
}

I'm interested about how can I remove the multiple if/else statements (削除) while keeping a complexity of O(n) (削除ここまで) and still loop through the array only once.

This is a "follow up" to an answer I gave on this question : Summing up distinct elements in steps

Here are the OP's requirements :

"My current task is to find a score from an array where the highest/lowest scores have been taken away, and if the highest/lowest occur more than once (ONLY if they occur more than once), one of them can be added:

E.g. int[] scores = [4, 8, 6, 4, 8, 5] therefore the final addition will be ∑4,8,6,5=23.

Another condition of the task is that LINQ cannot be used, as well as any of the System.Array methods (you can see by my previously ask questions that has been a bit of a pain for me, since I solved this with LINQ in less than 5 minutes)."

public int CalculateScore(int[] scores)
{
 int lowestValue = int.MaxValue,
 highestValue = int.MinValue,
 ammountOfHighestValue = 1,
 ammountOfLowestValue = 1,
 finalScore = 0;
 foreach (int score in scores)
 {
 finalScore += score;
 if (score < lowestValue)
 {
 lowestValue = score;
 ammountOfLowestValue = 1; //We need to reset the ammount
 }
 else if (score > highestValue)
 {
 highestValue = score;
 ammountOfHighestValue = 1; //We need to reset the ammount
 }
 else if (score == lowestValue)
 ammountOfLowestValue++;
 else if (score == highestValue)
 ammountOfHighestValue++;
 }
 if (ammountOfHighestValue > 1)
 //This way, we keep the highest score once.
 finalScore -= ((ammountOfHighestValue - 1) * highestValue); 
 else
 finalScore -= highestValue; //The value is there once, we remove it.
 if (ammountOfLowestValue > 1)
 finalScore -= ((ammountOfLowestValue - 1) * lowestValue); //Same as highest
 else
 finalScore -= lowestValue;
 return finalScore;
}

I'm interested about how can I remove the multiple if/else statements (削除) while keeping a complexity of O(n) (削除ここまで) and still loop through the array only once.

This is a "follow up" to an answer I gave on this question : Summing up distinct elements in steps

Here are the OP's requirements :

"My current task is to find a score from an array where the highest/lowest scores have been taken away, and if the highest/lowest occur more than once (ONLY if they occur more than once), one of them can be added:

E.g. int[] scores = [4, 8, 6, 4, 8, 5] therefore the final addition will be ∑4,8,6,5=23.

Another condition of the task is that LINQ cannot be used, as well as any of the System.Array methods (you can see by my previously ask questions that has been a bit of a pain for me, since I solved this with LINQ in less than 5 minutes)."

public int CalculateScore(int[] scores)
{
 int lowestValue = int.MaxValue,
 highestValue = int.MinValue,
 ammountOfHighestValue = 1,
 ammountOfLowestValue = 1,
 finalScore = 0;
 foreach (int score in scores)
 {
 finalScore += score;
 if (score < lowestValue)
 {
 lowestValue = score;
 ammountOfLowestValue = 1; //We need to reset the ammount
 }
 else if (score > highestValue)
 {
 highestValue = score;
 ammountOfHighestValue = 1; //We need to reset the ammount
 }
 else if (score == lowestValue)
 ammountOfLowestValue++;
 else if (score == highestValue)
 ammountOfHighestValue++;
 }
 if (ammountOfHighestValue > 1)
 //This way, we keep the highest score once.
 finalScore -= ((ammountOfHighestValue - 1) * highestValue); 
 else
 finalScore -= highestValue; //The value is there once, we remove it.
 if (ammountOfLowestValue > 1)
 finalScore -= ((ammountOfLowestValue - 1) * lowestValue); //Same as highest
 else
 finalScore -= lowestValue;
 return finalScore;
}

I'm interested about how can I remove the multiple if/else statements (削除) while keeping a complexity of O(n) (削除ここまで) and still loop through the array only once.

Missed part of the quote. Oops.
Source Link
RubberDuck
  • 31.2k
  • 6
  • 73
  • 176

This is a "follow up" to an answer I gave on this question : Summing up distinct elements in steps

Here are the OP's requirements :

"My current task is to find a score from an array where the highest/lowest scores have been taken away, and if the highest/lowest occur more than once (ONLY if they occur more than once), one of them can be added:

E.g. int[] scores = [4, 8, 6, 4, 8, 5] therefore the final addition will be ∑4,8,6,5=23.

Another condition of the task is that LINQ cannot be used, as well as any of the System.Array methods (you can see by my previously ask questions that has been a bit of a pain for me, since I solved this with LINQ in less than 5 minutes)."

Another condition of the task is that LINQ cannot be used, as well as any of the System.Array methods (you can see by my previously ask questions that has been a bit of a pain for me, since I solved this with LINQ in less than 5 minutes)."

public int CalculateScore(int[] scores)
{
 int lowestValue = int.MaxValue,
 highestValue = int.MinValue,
 ammountOfHighestValue = 1,
 ammountOfLowestValue = 1,
 finalScore = 0;
 foreach (int score in scores)
 {
 finalScore += score;
 if (score < lowestValue)
 {
 lowestValue = score;
 ammountOfLowestValue = 1; //We need to reset the ammount
 }
 else if (score > highestValue)
 {
 highestValue = score;
 ammountOfHighestValue = 1; //We need to reset the ammount
 }
 else if (score == lowestValue)
 ammountOfLowestValue++;
 else if (score == highestValue)
 ammountOfHighestValue++;
 }
 if (ammountOfHighestValue > 1)
 //This way, we keep the highest score once.
 finalScore -= ((ammountOfHighestValue - 1) * highestValue); 
 else
 finalScore -= highestValue; //The value is there once, we remove it.
 if (ammountOfLowestValue > 1)
 finalScore -= ((ammountOfLowestValue - 1) * lowestValue); //Same as highest
 else
 finalScore -= lowestValue;
 return finalScore;
}

I'm interested about how can I remove the multiple if/else statements (削除) while keeping a complexity of O(n) (削除ここまで) and still loop through the array only once.

This is a "follow up" to an answer I gave on this question : Summing up distinct elements in steps

Here are the OP's requirements :

"My current task is to find a score from an array where the highest/lowest scores have been taken away, and if the highest/lowest occur more than once (ONLY if they occur more than once), one of them can be added:

E.g. int[] scores = [4, 8, 6, 4, 8, 5] therefore the final addition will be ∑4,8,6,5=23.

Another condition of the task is that LINQ cannot be used, as well as any of the System.Array methods (you can see by my previously ask questions that has been a bit of a pain for me, since I solved this with LINQ in less than 5 minutes)."

public int CalculateScore(int[] scores)
{
 int lowestValue = int.MaxValue,
 highestValue = int.MinValue,
 ammountOfHighestValue = 1,
 ammountOfLowestValue = 1,
 finalScore = 0;
 foreach (int score in scores)
 {
 finalScore += score;
 if (score < lowestValue)
 {
 lowestValue = score;
 ammountOfLowestValue = 1; //We need to reset the ammount
 }
 else if (score > highestValue)
 {
 highestValue = score;
 ammountOfHighestValue = 1; //We need to reset the ammount
 }
 else if (score == lowestValue)
 ammountOfLowestValue++;
 else if (score == highestValue)
 ammountOfHighestValue++;
 }
 if (ammountOfHighestValue > 1)
 //This way, we keep the highest score once.
 finalScore -= ((ammountOfHighestValue - 1) * highestValue); 
 else
 finalScore -= highestValue; //The value is there once, we remove it.
 if (ammountOfLowestValue > 1)
 finalScore -= ((ammountOfLowestValue - 1) * lowestValue); //Same as highest
 else
 finalScore -= lowestValue;
 return finalScore;
}

I'm interested about how can I remove the multiple if/else statements (削除) while keeping a complexity of O(n) (削除ここまで) and still loop through the array only once.

This is a "follow up" to an answer I gave on this question : Summing up distinct elements in steps

Here are the OP's requirements :

"My current task is to find a score from an array where the highest/lowest scores have been taken away, and if the highest/lowest occur more than once (ONLY if they occur more than once), one of them can be added:

E.g. int[] scores = [4, 8, 6, 4, 8, 5] therefore the final addition will be ∑4,8,6,5=23.

Another condition of the task is that LINQ cannot be used, as well as any of the System.Array methods (you can see by my previously ask questions that has been a bit of a pain for me, since I solved this with LINQ in less than 5 minutes)."

public int CalculateScore(int[] scores)
{
 int lowestValue = int.MaxValue,
 highestValue = int.MinValue,
 ammountOfHighestValue = 1,
 ammountOfLowestValue = 1,
 finalScore = 0;
 foreach (int score in scores)
 {
 finalScore += score;
 if (score < lowestValue)
 {
 lowestValue = score;
 ammountOfLowestValue = 1; //We need to reset the ammount
 }
 else if (score > highestValue)
 {
 highestValue = score;
 ammountOfHighestValue = 1; //We need to reset the ammount
 }
 else if (score == lowestValue)
 ammountOfLowestValue++;
 else if (score == highestValue)
 ammountOfHighestValue++;
 }
 if (ammountOfHighestValue > 1)
 //This way, we keep the highest score once.
 finalScore -= ((ammountOfHighestValue - 1) * highestValue); 
 else
 finalScore -= highestValue; //The value is there once, we remove it.
 if (ammountOfLowestValue > 1)
 finalScore -= ((ammountOfLowestValue - 1) * lowestValue); //Same as highest
 else
 finalScore -= lowestValue;
 return finalScore;
}

I'm interested about how can I remove the multiple if/else statements (削除) while keeping a complexity of O(n) (削除ここまで) and still loop through the array only once.

Quote block
Source Link
RubberDuck
  • 31.2k
  • 6
  • 73
  • 176
Loading
added 62 characters in body
Source Link
IEatBagels
  • 12.7k
  • 3
  • 48
  • 99
Loading
edited tags
Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141
Loading
Source Link
IEatBagels
  • 12.7k
  • 3
  • 48
  • 99
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /