Home > Back-end >  How to get the first soonest occurrence between a use of a List and Dictionary
How to get the first soonest occurrence between a use of a List and Dictionary

Time:11-09

The list is unsorted with the values that come first second and third (1,2,3)
The list indicates the order of the values which is first,second or third.
The Row's are what I call the Keys of the Dictionary
The Values of the dictionary are values that are used in the list

(Since Row:200 has the smallest soonest Value:2) (based on newList) 2 is before 3.,
The Value:3 in Row:100 is later then Value:2, that's why the output should be Row:200.
After Row:200 is resolved, the next output would be Row:300 and Row:100 is the last outputted value, but I only need output one result not all 3.

Keys are Rows
Values are Values

Output is the Key of Dictionary

    Public Module Program
        Public Sub Main(args() As String)

            'This kinda works I guess
            Dim newList2 As New List(Of Byte)({1, 2, 3})
            Dim ListOfValues2 As New Dictionary(Of Integer, Byte)
            ListOfValues2.Add(100, 3)
            ListOfValues2.Add(200, 2)
            ListOfValues2.Add(300, 1)
    
        Dim firstToProcessRow As Integer = 0
        For Each value In newList2
            For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues2
                If ListOfValues2.ContainsKey(Row.Value) AndAlso ListOfValues2(Row.Value) = value OrElse Row.Key = 0 Then
                    firstToProcessRow = Row.Key
                    GoTo exitFor
                End If
            Next
        Next
exitFor:

        Debug.WriteLine(firstToProcessRow)
        'Output is 100 when it should be 200.

        End Sub
    End Module

CodePudding user response:

Solved using a scoring system

Basically this is a type of problem that can't be solved in one pass. You need to store some temp results and then process those.

"It seems like you're trying to figure out which value has the lowest 'score', where the score is based off the index of where the common value is in each array"

Here is the solution

   If newList.LongCount > 0 Then
        Dim scores As Integer() = New Integer(ListOfValues.Count - 1) {}
        Dim scoreCounter As Integer = 0
        Dim highestScore As Integer = -1

        For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues
            Dim key As Integer = Array.IndexOf(newList.ToArray(), Row.Value)
            scores(scoreCounter) = key
            scoreCounter  = 1
            If key > highestScore Then highestScore = key
        Next

        Debug.WriteLine("scores")
        Debug.WriteLine("Highest Score: " & highestScore)
        If highestScore <> -1 Then
            Dim firstLowestValue As Integer = highestScore
            scoreCounter = 0

            For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues
                If scores(scoreCounter) <> -1 AndAlso scores(scoreCounter) < firstLowestValue Then
                    Debug.WriteLine("Lowest = " & newList(scores(scoreCounter)) & " Lowest Row = " & Row.Key)
                    Return Row.Key
                End If
                scoreCounter  = 1
            Next
            firstLowestValue = highestScore
            scoreCounter = 0
            For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues
                If scores(scoreCounter) <> -1 AndAlso scores(scoreCounter) <= firstLowestValue Then
                    Debug.WriteLine("Lowest2 = " & newList(scores(scoreCounter)) & " Lowest Row = " & Row.Key)
                    Return Row.Key
                End If
                scoreCounter  = 1
            Next

            If firstLowestValue = highestScore Then Return ListOfValues.Keys.Min()
        End If
    Else
        Return CurrentRow
    End If
  • Related