- Home /
How to remove duplicate values from a list
I searched online for the answer but I just can't seem to find a simple solution for my issue. I'm working on a grid based puzzle game and I can't figure out how to handle multiple magnets magnetizing the same metal object. I calculate the empty space between the metal object and magnets and insert that value into a list. After a slight delay to make sure all magnets register, I want to remove all values with duplicates and find the smallest value from the remaining values. The LINQ Distinct().ToList() method doesn't work, as it just removes the duplicates, but I want all values with duplicates to be removed from the list. I tried using multiple for loops and 2 lists that would cross check each other, but all that didn't work either.
Answer by jackmw94 · Mar 22, 2021 at 09:59 AM
If you like LINQ then set your list to be equal to the list WHERE the COUNT of each value is 1:
list = list.Where(i => list.Count(j => i == j) == 1).ToList();
This i == j check can be replaced by whatever equality check you need.
Then to get the smallest value from the remaining values, sort the values in ascending order then take the first:
list = list.OrderBy(i => i).ToList();
int smallest = list.FirstOrDefault();
Again, replace "i => i" with whatever selector you use to determine what's smallest, i.e. if you have a magnet class with a magnet power property you can use "i => i.MagnetPower" . Once you're happy with these, you can daisy chain them to make a single call that just gets the smallest to prevent assigning all intermediary steps.
Hope this helps!
Sorry, but I'm quite new to coding (and LINQ), so I don't understand what I'm supposed to do with that code. I have a list of ints that are the empty spaces in between all of the magnets trying to magnetize the object. Let's say that list is "5, 4, 3, 5". So if I use your code "emptySlotList= emptySlotsList.Where(i => emptySlotList.Count(j => i == j) == 1).ToList();", I'm not quite sure how I'm supposed to replace the other values, so that I'm left with the values "4, 3".
It looks like you've made the right substitutions there! Let's break those lines down:
The notation with the "=>" arrow is a lambda expression, it's like a function that you write super small and in-line. These aren't a special linq thing either, you can use these elsewhere in your code too. Here's a lambda expression to calculate a value's third power:
Func<float, float> thirdPower = f => f * f * f;
float result = thirdPower(4);
// result = 64 = 4 * 4 * 4
In the Where function, the lambda expression is used to deter$$anonymous$$e whether we want to keep a value or not, it takes whatever type is in the list as its argument and expects that to result in a bool.
Then the Count function counts all elements for which the lambda expression evaluates to true. So if your list contained "0,1,2,3,4,5" and your count expression was list.Count(i => i < 3.5f) then it would return 4 as 0,1,2 and 3 would be counted as they are less than 3.5 but 4 and 5 wouldn't.
So the Where function looks at each element in the list and deter$$anonymous$$es whether to keep it based on whether the Count of elements of that value in the list equals 1. Count would evaluate like this for each value it gets passed:
5 => 2
4 => 1
3 => 1
5 => 2
So the Where would then look at 5 and say its Count (2) is NOT == to 1 and therefore we don't want it, 4's Count (1) IS == to 1 and therefore we do want it, etc. I'd expect the result of that line using the substitutions you wrote to result in emptySlotList containing just 4 and 3.
I somehow messed it up earlier, but now it works. Thank you so much! I never knew what lambda expressions meant before either, so thanks for the in depth explanation!