duplicate word removal

Terabojin

Active member
Joined
Mar 4, 2012
Messages
33
Programming Experience
Beginner
I am trying to accomplish this problem:

write an appication that inputs a sentace from user (assume no punctuation), then determines and displays the non-duplicate words in alphabetical order. Treat upper and lowercase the same. [can use String method Split with no arguments, as in sentence.Split() to break a sentence into an array of Strings containing the individual words. By default, Split uses spaces as delimiters. Use String method ToLower in the Select and Order By clauses of your LINQ query to obtain the lowercase version of each word.]

so far this is what i have:


 
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim sWords() As String = Split(TextBox1.Text, " ")
        'two counters to compare all words to eachother
        For iFirstCounter As Integer = 0 To UBound(sWords)
            For iSecondCounter As Integer = 0 To UBound(sWords) 'If we are looking at different words (different counter numbers)
                'and the words are the same, set the second one to nothing
                If iFirstCounter <> iSecondCounter And _
                    sWords(iFirstCounter) = sWords(iSecondCounter) Then
                    sWords(iSecondCounter) = ""
                End If
            Next
        Next
        'run by each word from the array
        For Each sWord As String In sWords
            'if the word is something, then add the word to the text and add a space at the end
            If sWord <> "" Then
                TextBox1.Text &= sWord & " "
            End If
        Next
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'clear the text box
        TextBox1.Text = ""
    End Sub
End Class





am i on the right track? if not what am i doing wrong. If I am on the right track, what am i missing?
 
[can use String method Split with no arguments, as in sentence.Split() to break a sentence into an array of Strings containing the individual words. By default, Split uses spaces as delimiters. Use String method ToLower in the Select and Order By clauses of your LINQ query to obtain the lowercase version of each word.]
You don't need ToLower on each word, as shown before in this thread you can do that on the string input once. You also don't need a Select call when it is the From items you're selecting. Then you apply Distinct to get unique items and finally Order By to sort them. This example displays the case-insensitive unique sorted words in a ListBox:
Me.ListBox1.DataSource = (From word In Me.TextBox1.Text.ToLower.Split Distinct Order By word).ToArray
 
Back
Top