e.g) I want this list [1, 2, 5, 10, 1, 2, 4] to [1, 2, 5, 10, 4]
What kind of function should I use to remove same value(key) on list?
Please help me.
thanks.
CodePudding user response:
You can use the Enumerable.Distinct Method like so:
List<int> list = new List<int> { 1, 2, 5, 10, 1, 2, 4 };
list = list.Distinct().ToList(); // 1, 2, 5, 10, 4
Note that the documentation does not give a guarantee that the order of the sequence is preserved. However, the Distinct method is implemented such that the order is indeed preserved, as can be seen in the source.
You may also consult this post: Does Distinct() method keep original ordering of sequence intact?