Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7735535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:31:36+00:00 2026-06-01T07:31:36+00:00

I have already made an algorithm for the problem but it still got a

  • 0

I have already made an algorithm for the problem but it still got a trouble.

Suppose that I have the recap of schedule (contains on gridview named GV):

TimeStart  TimeEnd   TotalOccuredOnThisTime
----------------------------------------------
08.00      08.50     1
08.00      09.40     43
08.00      10.50     2

Pls take a look to the picture below for more clear information:
enter image description here

What I want to get from the algorithm is, to count the time that occured on the same period, e.g. on the time of 08.00, it’s occured 46 event (see the yellow colored row).

This is my algorithm:

Dim ColumnLength As Integer = GV.Rows.Count
Dim TimeStart(ColumnLength - 1) As Integer
Dim TimeEnd(ColumnLength - 1) As Integer
Dim Total(ColumnLength - 1) As Integer

For i = 0 to ColumnLength - 1
  TimeStart(i) = Convert.ToInt32(Regex.Replace(GV.Rows(i).Cells(0).Text, ".", ""))
  TimeEnd(i) = Convert.ToInt32(Regex.Replace(GV.Rows(i).Cells(1).Text, ".", ""))
  Total(i) = 0
Next 

For i = 0 To ColumnLength - 2
  For j = 0 To ColumnLength - 1        
    If TimeEnd(i) > TimeStart(j) And ((TimeStart(i) <= TimeEnd(j)) Or (TimeStart(i) >= TimeEnd(j))) 
      Total(j) = Total(j) - Total(i)
    End If
  Next
Next

But it’s resulted the wrong value.

The result what I want is like:

Time: 08.00 - 08.50   --> 46 event occured
      08.50 - 09.40   --> 46 event occured
      09.40 - 10.00   --> 45 event occured
      etc...

How to do that properly? Really need your help…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:31:38+00:00Added an answer on June 1, 2026 at 7:31 am

    You can use a Dictionary to keep track of the time ranges by concatenating the two time strings together for the Key (no need to cast to integers), and then for each occurrence of that range you could increment the Value portion of the Dictionary.

    Additionally, there is no need to have nested loops using this approach, or a staging loop to populate start and end time arrays. The following algorithm should provide the results you are looking for:

    Dim columnLength As Integer = GV.Rows.Count
    Dim timeRangeCount As New Dictionary(Of String, Integer)
    
    For i = 0 to columnLength - 1
      Dim timeStart As String = GV.Rows(i).Cells(0).Text
      Dim timeEnd As String = GV.Rows(i).Cells(1).Text
      Dim key As String = String.Format("{0}-{1}", timeStart, timeEnd)
    
      If timeRangeCount.ContainsKey(key)
          timeRangeCount(key) += 1
      Else
          timeRangeCount(key) = 1
      End If
    Next
    

    Thus you could pull the count of the time range 09.40 – 10.00, for example, from the dictionary by saying:

    Dim totalOccuredOnThisTime As Integer = timeRangeCount("09.40-10.00")
    

    I’ve verified this algorithm with the similar test code:

    Dim columnLength As Integer = 6
    Dim timeStart() As String = {"08.00", "08.00", "10.00", "08.00", "08.00", "10.00"}
    Dim timeEnd() As String = {"08.50", "08.50", "11.00", "09.00", "08.50", "11.00"}
    Dim timeRangeCount As New Dictionary(Of String, Integer)
    
    For i = 0 to columnLength - 1
      Dim key As String = String.Format("{0}-{1}", timeStart(i), timeEnd(i))
    
      If timeRangeCount.ContainsKey(key)
          timeRangeCount(key) += 1
      Else
          timeRangeCount(key) = 1
      End If
    Next
    
    Console.WriteLine(timeRangeCount)
    

    enter image description here

    Update

    After reading your clarification in your comment, you can still use the Dictionary to help keep track of this along with a couple helper classes (TimeRange, TimeRangeCounter, and TimeRangeEqualityComparer). This does make things a bit more complicated though.

    The first helper class is used to hold the counts of exact and sub range matches:

    Public Class TimeRangeCounter
        Property ExactRangeMatch as Integer
        Property SubRangeMatch as Integer
    End Class
    

    The second helper class is used to help the dictionary know how one key (of type TimeRange) differs from another:

    Public Class TimeRangeEqualityComparer 
        Implements IEqualityComparer(Of TimeRange)
    
        Public Overloads Function Equals(left As TimeRange, right As TimeRange) _
                As Boolean Implements IEqualityComparer(Of TimeRange).Equals           
    
            Return left.ToString = right.ToString   
        End Function
    
        Public Overloads Function GetHashCode(range As TimeRange) _
                As Integer Implements IEqualityComparer(Of TimeRange).GetHashCode
    
            return range.ToString().GetHashCode()
        End Function
    
    End Class
    

    The Third helper class stores the start and end times of a range:

    Public Class TimeRange 
        Private readonly _start
        Private readonly _end
    
        Public Readonly Property Start 
            Get
               return _start
            End Get
        End Property
    
        Public Readonly Property [End] 
            Get
               return _end
            End Get
        End Property
    
        Public Sub New(start As String, [end] As string)
            Me._start = start
            Me._end = [end]
        End Sub
    
        Public Overrides Function ToString() as String
           Return String.Format("{0}-{1}", Start, [End])
        End Function
    
    End Class
    

    So using the above we should be able to write this algorithm:

    Dim columnLength As Integer = 5
    Dim timeStart() As String = {"08.00", "08.00", "10.00", "08.00", "08.00"}
    Dim timeEnd() As String = {"08.50", "11.50", "11.00", "09.00", "08.50"}
    Dim comparer As New TimeRangeEqualityComparer()
    Dim timeRangeCounts As New Dictionary(Of TimeRange, TimeRangeCounter)(comparer)
    
    'Count exact range matches while building dictionary
    For i = 0 to columnLength - 1
      Dim key As TimeRange = New TimeRange(timeStart(i), timeEnd(i))
    
      If timeRangeCounts.ContainsKey(key)
          timeRangeCounts(key).ExactRangeMatch += 1
      Else
          Dim counter =  New TimeRangeCounter()
          counter.ExactRangeMatch = 1
          timeRangeCounts(key) = counter
      End If        
    
    Next           
    
    'Count sub ranges          
    For Each kvp in timeRangeCounts
        For Each key in timeRangeCounts.Keys
            If kvp.key.Start >= key.Start AndAlso _ 
               kvp.Key.End <= key.End AndAlso _
               kvp.key.ToString <> key.ToString then           
    
                kvp.Value.SubRangeMatch += 1
            End If
        Next
    Next
    
    Console.WriteLine(timeRangeCounts)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem creating and executing a JAR file. I have already made
I have already made a regex which should work, but it doesn't work. echo
I have an XDocument class with the XML contents already made. I basically want
I have already posted something similar here but I would like to ask the
I have already created a webpart to show the data from list, but I
I have already posted a question about this, but the situation has changed sufficiently
I have checked out from the main trunk and made some changes that I
I know some have already asked the question but so far, the answers are
I have already made a function of multiplication of long numbers, addition of long
I am looking to create an app I have already made for Android for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.