How to remove all duplicate characters from a string in VB.NET

2 Answers

0 votes
Imports System
Imports System.Linq

Public Class Program
	Public Shared Function remove_duplicate_characters(ByVal s As String) As String
        Return New String(s.ToCharArray().Distinct().ToArray())
    End Function

    Public Shared Sub Main()
        Dim s As String = "vbvb ppprooooooogramminggg"
		
        s = remove_duplicate_characters(s)
		
        Console.Write(s)
    End Sub
End Class








' run:
'
' vb progamin
'

 



answered Sep 4, 2021 by avibootz
0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
	Public Shared Function remove_duplicate_characters(ByVal s As String) As String
        Dim hs = New HashSet(Of Char)(s)
        Return String.Join("", hs)
    End Function

    Public Shared Sub Main()
        Dim s As String = "vbvb ppprooooooogramminggg"
		
        s = remove_duplicate_characters(s)
		
        Console.Write(s)
    End Sub
End Class









' run:
'
' vb progamin
'

 



answered Sep 4, 2021 by avibootz
...