How to get the total numbers with no repeated digits from a range of numbers in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class [MyClass]
    Public Shared Function repeated_yes_0_no_1(ByVal n As Integer) As Integer
        Dim hs As HashSet(Of Integer) = New HashSet(Of Integer)()

        While n <> 0
            Dim digit As Integer = n Mod 10

            If hs.Contains(digit) Then ' repeated = yes
                Return 0
            End If

            hs.Add(digit)
            n = n \ 10
        End While

        Return 1
    End Function

    Public Shared Function GetTotalNumbersWithNoRepeatedDigits(ByVal start As Integer, ByVal _end As Integer) As Integer
        Dim total As Integer = 0

        For i As Integer = start To _end
            total += repeated_yes_0_no_1(i)
        Next

        Return total
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim start As Integer = 1, _end As Integer = 100

        Console.WriteLine(GetTotalNumbersWithNoRepeatedDigits(start,_end))
    End Sub
End Class




' run
'
' 90
'


 



answered Jan 14, 2023 by avibootz
edited Jan 14, 2023 by avibootz
...