0

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.

3
  • Possible duplicate of stackoverflow.com/questions/14636142/linq-distinct-and-orderby Commented Aug 27, 2020 at 5: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? Commented Aug 27, 2020 at 6: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. Commented Aug 29, 2020 at 3:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.