Update a Dictionary

Hello EE,

I have a list of integer. It could be very big, it depends.
I need to Loop through it, and I want to update the value my dictonary with all the keys of the list to 0.
at the same time, ALL the other keys that were not in that list, I want them to be incremented of 1.

I cannot do 2 loops because its slow...  I would like to optimize the maximum possible the way I Loop and update the values....
can you help ?
also... I know I cannot update 2 times a dictionary by looping I think it created an error...
LVL 1
PhilippeRenaudAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
You could:
(1) Sort your List of Integers.
(2) Iterate over the Dictionary using a KeyValuePair.
(3) For each KeyValuePair, see if the Key is present in the List using List.BinarySearch().
(4) Update the Value to 0 (zero) or Increment based on the result of (3).

Here we are technically not looping twice, but we are spending time up front sorting the List.

No matter what, you're going to have to spend some extra processing time figuring out which items were not in the List...it's just a matter of how you figure that out.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PhilippeRenaudAuthor Commented:
Well ok i will try... but I tried to summerize what I wanted.. I actually do not have a list but a result of Facet.Combinatorics  

I use Dim g2 As New Combinations(Of String)....
and i am not sure how ot Sort That results...
0
PhilippeRenaudAuthor Commented:
I managed to convert my result to a lsit of string.
i did your binarysearch like this:

        Dim g2 As New Combinations(Of String)(tmpResult2, Convert.ToInt16(txtNumbers.Text.Trim()), GenerateOption.WithoutRepetition)
        Dim a = (From s In g2
                 Select s(0) + "," + s(1)).ToList()

        For Each kvp As KeyValuePair(Of String, Integer) In finalDict
            If a.BinarySearch(kvp.Key) Then
                finalDict(kvp.Key) = 0
            Else
                finalDict(kvp.Key) += 1
            End If
        Next

im having error like i thought

An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

Additional information: Collection was modified; enumeration operation may not execute.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.