I have an array of object that contains property like rank, value etc. I want to find the object which having highest rank and doesn't have duplicate using Linq c#, I mean that should be unique.
Asked
Active
Viewed 234 times
0
-
Possible duplicate of https://stackoverflow.com/questions/14636142/linq-distinct-and-orderby – Kaveesh Aug 27 '20 at 05:33
-
_"having highest rank and doesn't have duplicate"_ what does that mean exactly? Can you post some little code? Does it mean the array does not contain duplicate entries of the same instances or rank property is unique in all entries or you want the one with highest unique rank or something completely different? As I read it, you are expecting out of the ranks: [0, 1, 1, 2, 3, 4, 5, 5, 6, 6, 6] to return 4 since it is the highest rank that has no duplicates? – Fildor Aug 27 '20 at 06:44
-
[0, 1, 1, 2, 3, 4, 5, 5, 6, 6, 6] to return 4 since it is the highest rank that has no duplicates. yes I want the same. – Radhika Aug 29 '20 at 03:24
1 Answers
2
int[] numbers = { 1, 2, 2, 3, 3, 4, 4, 5, 6, 6, 6 };
int maxUniqueNumber = numbers.GroupBy(n => n)
.Where(g => g.Count() == 1)
.Select(g => g.Key)
.Max();
Credit to: C# Get non duplicates in a list for the unique number part of this answer.

bar9833625
- 295
- 2
- 11
-
While this works for integers, OP has an array of some class with an int property. And he wants that instance with the highest value in that property. – Fildor Aug 27 '20 at 06:42