Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
C#
List<int> list = new List<int>() { 1, 1, 2 };
            List<int> list1 = new List<int>() { 1, 2 };
            List<int> list2 = (list.Except(list1)).ToList();
            foreach (int item in list2)
            {
                Console.WriteLine(item);
            }


What I have tried:

what I am trying to do is take an input list from the user and checking if there are any duplicates in it and then print which elements are duplicates.

So basically My List1 is the user input and List2 is the List1.Distinct() (I am trying this by hardcoding just to understand how can I extract the duplicate values)

So basically if my input list is like: {1,2,3,4,2,1,5}
Output I am expecting is {1,2} since 1,2 have duplicate values
Posted
Updated 12-Jul-23 21:52pm
v4
Comments
PIEBALDconsult 11-Jul-23 9:36am    
Aaaaand... what do you get?
Samriddha Chowdhury 11-Jul-23 9:40am    
Hi the list2 is empty and prints nothing. How to achieve the result I want? Mind you that I am very new to programming and dont have much experience
PIEBALDconsult 11-Jul-23 11:17am    
I think you need to improve the question to add more detail, such as what you want to have happen when the lists are { 2 , 1 , 1 } and { 1 , 2 } ? The second sequence does not appear in the first sequence.
BillWoodruff 11-Jul-23 11:45am    
specify exactly the output you want to see
PIEBALDconsult 11-Jul-23 16:24pm    
Oh, and what is the expected result of f ( { 1 , 1 , 2 , 2 } , { 1 , 2 } ) and why?

There's nothing wrong with your code; it's your understanding of the Except method that's at fault. :)

Except returns "the set difference of the elements of two sequences". That means it returns a distinct set of elements which only exist in one of the input sequences.

In your example, all elements from the first list exist in the second, and all elements from the second list exist in the first. Therefore, the difference is the empty set, and there will be no elements to output.
 
Share this answer
 
Comments
Samriddha Chowdhury 11-Jul-23 9:44am    
Thank you sir. Could you please help me to rectify the code so that I can get the desired output?
Richard Deeming 11-Jul-23 9:49am    
It's not a standard LINQ operator; you'll need to remove the items one-by-one:
List<int> list2 = new List<int>(list);
foreach (int i in list1)
{
	list2.Remove(i);
}
PIEBALDconsult 11-Jul-23 10:36am    
Won't that have the same effect?
He doesn't want to remove all 1s and 2s.
Richard Deeming 11-Jul-23 10:37am    
No; Remove will remove the first instance of the specified value from the list.

Demo[^]
Samriddha Chowdhury 11-Jul-23 10:41am    
yes that is correct. I have changed my code like this

List<int> list = new List<int>() { 1, 1, 2 };
List<int> list1 = new List<int>() { 1, 2 };
List<int> list2 = new List<int>(list);
foreach (int i in list1)
{
list2.Remove(i);
}


and it is also producing empty list

Could you please make the changes in my code itself?
This is a fairly simple matter to fix:
C#
List<int> list1 = new List<int>() { 1, 1, 2 };
List<int> list2 = new List<int>() { 1, 2 };

foreach (int item in list2)
{
    if (list1.Contains(item))
    {
        list1.Remove(item);
    }
}
 
Share this answer
 
Comments
PIEBALDconsult 11-Jul-23 11:54am    
Unsure that's actually what is desired, because the question isn't clear enough.
Richard MacCutchan 11-Jul-23 12:24pm    
It looks clear enough to me. He has [1,1,2] in the first list and [1,2] in the second. He wants to remove one item from the first list that matches each from the second: i.e. end up with [1].
PIEBALDconsult 11-Jul-23 12:28pm    
I disagree, I think he wants to remove the _sequence_ { 1 , 2 } from { 1 , 1 , 2 }
Richard MacCutchan 11-Jul-23 12:45pm    
That's what the code above does, and what I just said in my previous message.
PIEBALDconsult 11-Jul-23 13:37pm    
What does yours do with the inputs { 2 , 1 , 1 } and { 1 , 2 } ?
Quote:
what I am trying to do is take an input list from the user and checking if there are any duplicates in it and then print which elements are duplicates.

So basically My List1 is the user input and List2 is the List1.Distinct() (I am trying this by hardcoding just to understand how can I extract the duplicate values)

So basically if my input list is like: {1,2,3,4,2,1,5}
Output I am expecting is {1,2} since 1,2 have duplicate values

Here is a different approach:
C#
List<int> list1 = new List<int>() { 1, 2, 3, 4, 2, 1, 5 };

List<int> duplicates = list1
    .GroupBy(x => x)
    .Where(x => x.Count() > 1)
    .Select(x => x.Key)
    .ToList();

Console.WriteLine(string.Join(',', duplicates));

Output:
1,2

What it does is
1. generate a grouping of unique values, with all of the same values
2. then use the count to identify duplicates
3. only select those duplicates
4. output the results as a list of integers

The last line joins all the results and outputs to the console.
 
Share this answer
 
Comments
Richard MacCutchan 13-Jul-23 4:09am    
A much more elegant solution than mine. I did try using LINQ but I am so poor at it ...
Graeme_Grant 13-Jul-23 4:45am    
Thanks, to me it suits the requirements.
Samriddha Chowdhury 13-Jul-23 11:29am    
Thank you sir. Since I am quite new so I don't feel comfortable with LINQ yet. So I am using the foreach loop solution since I understand it more easily

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