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.
Asked
Active
Viewed 43 times
0
-
1Consider using a `HashSet` instead of `List` – Charlieface May 22 '22 at 16:47
-
1Do you need to preserve the order of the elements? – Caius Jard May 22 '22 at 17:04
-
no just remove same keys. someone solved this problem. but thanks to help me – 222 2 May 23 '22 at 16:10
-
Please use the correct tags! Note that [`[unityscript]`](https://stackoverflow.com/tags/unityscript/info) is or better **was** a custom JavaScript flavor-like language used in early Unity versions and is **long deprecated** by now. It sounds like you are asking about a solution `c#` ;) – derHugo Jun 01 '22 at 09:36
1 Answers
3
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?

kmschaal
- 66
- 3