This question already has answers here:
C# LINQ find duplicates in List
(13 answers)
Closed 5 years ago.
I was hoping to do something like:
int[] locations;
do
{/*edit locations to only contain one int in array of same value*/}
while (locations.Where(val, secondVal => val == secondVal).count > 1)
And I highly feel like I've done something of the sort in the past, perhaps it was with a list and it doesn't function the same way with an array but I can't seem to find my example anywhere.
I feel that the code snip explains well enough my intentions, but it doesn't work, would someone be able to help? Thanks.
I'll keep searching online but if someone could help it would obviously speed up my search.
You should look to this answer:
C# LINQ find duplicates in List
Just group elements by its value and then get the list
GroupBy(x=>x).Where(g=>g.Count()>1)
Related
This question already has answers here:
The best way to get a count of IEnumerable<T>
(11 answers)
Closed 2 years ago.
I want to be able to test a count of obj below but not able to do so.
when I mouse over obj it shows a count of 3
<IEnumerable<File> obj = await _mytester.cleanup(myitems)
why cant i do this?
if(obj.count > 0)
The liine above is giving me an error
You can't do that because count doesn't exist on IEnumerable and that's by design.
You can use IEnumerable.Count() like this:
if (obj.Count() > 0)
But that's horribly inefficient since you're really just checking if there's anything there.
You're better off doing this:
if (obj.Any())
Both of these are provided by LINQ extension methods.
It's very important to note that Count() is iterating through all elements and is nowhere near as efficient as something like ICollection.Count
This question already has answers here:
How to find if an element of a list is in another list?
(5 answers)
Closed 9 years ago.
I apologize if this is an obvious question, but I cannot find the answer.
Say I have the following:
var list1 = new List<int>{1,2,3};
var list2 = new List<int>{3,5,6};
How can I see if ANY element of list1 is contained in list2? So in this case I want to return true because 3 is in both.
Performing nested loops will not work for me, so it would be ideal if there was a:
list1.HasElementIn(list2);
Use Enumerable.Intersect - it produces intersection of both sequences. If intersection is not empty, then there some item which exists in both sequences:
bool isAnyItemInBothLists = list1.Intersect(list2).Any();
One thing to note - thus Intersect is a deferred streaming operator, then you will get result as soon as any common item will be found. So, you don't need to wait until complete intersection will be computed.
This question already has answers here:
MongoDB: Is it possible to make a case-insensitive query?
(27 answers)
Closed 8 years ago.
I have a collection like
{
"email" : "sh#Gmail.com"
}
My query should be like , it has to find matching email irrespective of case whether its capital or small but it should match exactly.
In sql we will do like where Lower(strEmail) = Lower(#emailParameterPassed) to satisfy this same.
UPDATE
I got it here.
Thanks
How to achieve this in mongoDb?
I am using c# native driver with mongoDb ?
Nonetheless the answer you are duplicating will give you a correct result, it is not a good idea to do queries like this. Why should you use regex (without indexes ) if you can use normal equal and take advantage of indexes?
There is no difference between sh#Gmail.com and sh#gmail.com so why not to store them in a canonical form in the first place and then to use normal search. To change all the documents, you can refer to my previous answer.
This question already has answers here:
Is there a Lower Bound function on a SortedList<K ,V>?
(6 answers)
Closed 9 years ago.
I need a built-in datastructure in C# that has functionality similar to std::set (or std::map) in c++. What is important to me is that the structure should be sorted(thus Dictionary will not do here) and has a method similar to lower_bound(i.e. return first element with value at least v). I will also need to insert and delete elements from the structure. If possible I need these operations to have complexity O(log(n)). Could you please point me to the appropriate datastructure in C#?
I suspect you are looking for SortedSet in terms of a similar data structure to std::set.
This article goes into its performance characteristics.
A lower_bound functionality seems to be possible using SortedSet.GetViewBetween but has a worse complexity than the desired O(log(n)) according to this discussion.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to calculate or approximate the median of a list without storing the list
I want to apply using C# an algorithm to find the median value using selection/quick sort. But I do not want to sort the whole array in order to get the median.
Can I do it?
Wikipedia's entry on Selection Algorithm gives various alternatives, including the Median of Medians approach, which would seem to fit your requirements. In particular, it has a worst-case performance of O(n).