Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have a list1 = {1,1,2,3,4,5}

I want to compare the items in the list.

My code:
var list2 = list1.GroupBy(c => c.action).Select(g => g.FirstOrDefault()).ToList();

Now i have a new list2 = {1,1,2}
i want to get a 2 out of this list.


or if List ={1,1}
I want to get 1 out of it.

Any suggestions, please?
Posted
Updated 15-May-13 19:19pm
v4
Comments
Sergey Alexandrovich Kryukov 14-May-13 12:11pm    
Your language? (No, I don't want to take a guess.)
—SA

You don't need a lambda - just use Linq methods:
C#
List<int> list1 = new int[] { 1, 1, 2, 3, 4, 5 }.ToList();
List<int> list2 = new int[] { 1, 1, 2 }.ToList();


var result = list1.Intersect(list2);
foreach (int i in result) Console.WriteLine(i);
 
Share this answer
 
Comments
rohit24c 16-May-13 3:11am    
I need to get the items for a List if it occurs 1 in the List
List= {1,1,2}

i need 2 from the list as it occurs once.
OriginalGriff 16-May-13 3:26am    
AH! You need to explain your needs better - remember we can't see your screen or access your HDD...
Try:
int i = list1.Intersect(list2).Count();
C#
int[] arr = new int[] { 1, 1, 2 };
//Create a dictionary where the key is each int occurance
//and the value is the number of int occurances.
Dictionary<int, int> dict = arr.Distinct().ToDictionary(i => i, i => arr.Where(ii=>ii==i).Count());
//now do what you like with it.. 
//eg get me the value that occurs least often, 
//which I think is what you're after:
int r=dict[dict.Values.Min()];


ATG
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900