1

I am trying to remove the minimum value of a List, the main() function calls the removeSmallest() function and Writelines the return List, but all I get is:

"System.Collection.Generic.List`1[System.Int32]"

Varibles explanations: "n" --> counts how often the foreach-lopp ran in order to paste earlyer values in the List at the right time by saving them in, "posa" --> saves the spot for earlyer values to past them in the List at the right, "a" --> saves the previous number to compare it with the next one,

Lists: "numbers" --> is the input, "list" --> output,

You can find my Code here or [here][2]

THX for ur Help!!

[2]: http://ideone.com/Y6lUSC ideone.com

FailedUnitTest
  • 1,637
  • 3
  • 20
  • 43
Lukas
  • 49
  • 1
  • 7
  • 2
    http://stackoverflow.com/help/how-to-ask –  May 31 '16 at 14:32
  • [How to display list items on console window in C#](http://stackoverflow.com/questions/759133/how-to-display-list-items-on-console-window-in-c-sharp) – 001 May 31 '16 at 14:36

3 Answers3

3

Well, just Remove() a Min():

   List<int> numbers = new List<int>() {
     5, 3, 9, -2, 5, -2, 4 // please, notice that -2 appeared twice
   };

   // Just do this
   numbers.Remove(numbers.Min());

   // Let´s print out the test:
   // 5, 3, 9, 5, 4
   Console.Write(String.Join(", ", numbers));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
3

LINQ can come in handy here, you can use this idea:

var numbers = new []{22,31,14,25,26,67,8,49,101};
var numbersNoLowest = numbers.OrderByDescending(x => x).Take(numbers.Count() - 1).ToList();
foreach(var number in numbersNoLowest)
{
    Console.WriteLine(number);    
}

Run here: http://csharppad.com/gist/0d9d0f87b7c3bdf28a4681f4a8ada241

FailedUnitTest
  • 1,637
  • 3
  • 20
  • 43
0

I am not sure what your code is trying to do, but it doesn't remove the minimum value. If you want to remove all occurrences of the minimum value & leave the remaining values in the original order (which is what partially happens in your code) the following should work :

  int min = numbers.Min();
  List<int> list = numbers.Where(val => val != min).ToList();
PaulF
  • 6,673
  • 2
  • 18
  • 29