Fred Mastropasqua's Facebook profile

VB.Net Find Missing Numbers in a Sequence

by Fred Mastro 4. December 2008 01:57

I can't remember where I found the original c# version of this. I can't take credit for the logic, I merely converted it to VB.Net and used it in my app, and it works great. I did alter it a little but only to use StringCollection object since it fit into my other code nicer.

 

I had a need to find the missing numbers in a sequence. I had used SQL to try and perform this task and had found something that worked great, until I realized it didn't work when two consecutive numbers were missing. Then it would only list one of those. So instead I had used VB.Net to get me the missing numbers.

1) Create a comma delimited list of the numbers in the sequence
2) Send it to the function and return back a StringCollection object

The object returned will only have the missing numbers, if any.

''' <summary>   
''' Returns the missing numbers in a sequence.   
''' </summary>   
''' <param name="strNumbers">Expects a string of comma-delimited numbers. ex: "1,2,4,6,7"</param>   
''' <returns></returns>   
''' <remarks></remarks>   
Public Function FindMissingNumbers(ByVal strNumbers As String) As StringCollection   
  Dim MissingNumbers As New StringCollection   
  Dim arNumbers() As String  
  arNumbers = Split(strNumbers, ",")  
  arNumbers = SortNumbers(arNumbers)  
 
  For I = 0 To UBound(arNumbers) - 1  
    If CLng(arNumbers(I)) + 1 <> CLng(arNumbers(I + 1)) Then  
      For J = 1 To (CLng(arNumbers(I + 1)) - CLng(arNumbers(I))) - 1  
        MissingNumbers.Add(CStr(CLng(arNumbers(I)) + J))  
      Next  
    End If  
  Next  
 
  Return MissingNumbers  
End Function  
 
''' <summary>  
''' Sorts the array of numbers in value order, least to greatest.  
''' </summary>  
''' <param name="arNumbers">Send in a string() array of numbers</param>  
''' <returns></returns>  
''' <remarks></remarks>  
Private Function SortNumbers(ByVal arNumbers() As String) As String()  
  Dim tmpNumber As String  
  For J = 0 To UBound(arNumbers) - 1  
    For I = J + 1 To UBound(arNumbers)  
      If arNumbers(I) - arNumbers(J) < 0 Then  
        tmpNumber = arNumbers(J)  
        arNumbers(J) = arNumbers(J)  
        arNumbers(I) = tmpNumber  
      End If  
    Next  
  Next  
 
  Return arNumbers  
End Function

So there you go.  The FindMissingNumbers calls the SortNumbers, notice it's Private.  You can call FindMissingNumbers and get back your Missing Numbers.

Tags: ,

Code Snippets | Programming | VB.NET

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

A Certified MCSE (NT4 & 2k), MCDBA (2k), A+, CCA, with over 10 years of experience with Windows Networking and Development. Developing mainly in ASP.NET, VB.NET and T-SQL. Also develops in Objective-C (iPhone), XAML (SilverLight & WPF), C#, "Classic" ASP 3.0, ADSI,  VBscript, WScript.

Non-technical hobbies include other areas such as Movie watching (action, epic, comedy, some romantic comedy, well everything), Reading (Science Fiction, Fantasy, Detective and Programming categories), Film Editing, Directing with Special effects (using Adobe Premiere and Adobe After Effects), Dungeons & Dragons (D&D 4th Edition), Auto-Cross Racing & Cars (BMW M3, MazdaSpeed's), Motorcycles (Honda CBR 600), TV Shows (Flight of the Conchords, Lie To Me, DollhouseBattleStar Galatica, Smallville, Alias), Music (Akon, Billy Joel, Micheal Bublé, Bid Daddy Weave, T-Pain, Barlow Girl, Notorious B.I.G and more, love all types of music), and Religion (Christianity, debating and prophecies).


Web Tools - QuickLinks

Web tools I use more then others. Some of these are on my Link Collections page, but this made it easier for me to go to my site and click a tool.

  1. Telerik Code Converter (C# to VB/VB to C#)
  2. Lorem Ipsum - Dummy Text for Prototype Apps
  3. Web Color Values
  4. Open Source Icons

 

Highlights

  • Some websites I've worked on. This is a small collection of sites I've developed or added to awhile back.

  • Revenge Movie Trailer. Trailer I made with Adobe Premiere and After Effects. Jason Christman is the main star and I'm the director behind the camera.

  • Essential Software For your Mac. - I'm a Microsoft geek, but I've switched over to Mac. There was a lot of stuff I needed to get installed that I missed on my Windows machine. Also I had no idea how to do it :p Here's some help.

  • Speed Football. I wanted to make a special effect like the Smallville or Superman running fast. All the other ones I've seen, the person in the frame was the only moving object while everything else was blurred. I wanted to create the effect but interact with another normal moving object.
  •    
  • Code Snippets and Quickies. Sometims I find something or develop something that I think is useful and it can be copied and pasted anywhere for someone to use. Here's a collection of things I've posted on.
  • Books I've Read or Reading