Resolved hashset to remove items

andrews

Well-known member
Joined
Nov 22, 2011
Messages
132
Programming Experience
5-10
My question.
I have hs as hashset(of integers)
And always f.e. with items 57,54,51,43,28,26,5,4 , the numbers are always ordered from high to law
How can I delete on the most fast way all the numbers starting from 51 to the last number
Thanks for any response
 
The HashSet(Of T) class has a RemoveWhere method that will remove all items that satisfy a specified condition, e.g.
VB.NET:
Dim itemRemovedCount = myHashSet.RemoveWhere(Function(n) n <= 51)
The order of the items is irrelevant in that case.
 
If the order bothers you, and instead of using orderby - you can simply use : SortedSet<T> Class (System.Collections.Generic) - However, you will need to be running .Net 4.0 minimum.
VB.NET:
        Dim mySortedSet As SortedSet(Of Integer) = New SortedSet(Of Integer)()
        mySortedSet.Add(1)
        mySortedSet.Add(12)
        mySortedSet.Add(102)
        mySortedSet.Add(103)
        mySortedSet.Add(95)
        mySortedSet.Add(40)
        mySortedSet.Add(80)

        mySortedSet.RemoveWhere(Function(Num) Num <= 51)
        For Each num As Integer In mySortedSet
            Console.WriteLine(num)
        Next
Output is the minimum value first :
VB.NET:
80
95
102
103
And when you say :
How can I delete on the most fast way all the numbers starting from 51 to the last number
If you want from 51 towards 100, then the above, otherwise change to >= 51)
 
Shouldn't that be the other way around?
Nop. Because
Function(Num) Num <= 51) Removes where the number is less than or equal to 51 which leave you :
80
95
102
103
Function(Num) Num >= 51) Removes where the number is greater than or equal to 51 which leave you :
1
12
40
It's the same logic as Linq. If you were to say Select<int>().Where<int>(NOT< or > than 51). Because the function above removes where the condition is boolean opposite. If you remove where the numbers are less than or equal a value, it will only leave you with the higher numbers.
Thanks very much for this solution.
You're welcome.
 
Back
Top