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?
 
Have you tried running this yet? This code shouldn't be in the textchanged handler.. as soon as the first letter is typed by the user, the code starts looking for words that are not there yet..
 
Yes I i did try running the code. I was only able to enter one letter into the text box before it startes spazzing out on me. however it wasnt throwing any errors out after running. thats why i thought id ask. where should i put it then. im still trying to grasp this and im not doing very well.
 
Add the code to a button or to the textbox's keypress event if the key pressed is the "enter" key for example..

So far your code is taking the sentence and removing duplicates as instructed so yes you are on the right track

PS Don't forget to clear the textbox before you write the non duplicate words back in to it.
 
ok which part do i need to move, the whole thing sans where ive written the code for the clear button (button2.click)
 
Take everything from the textchanged handler and move it to its new destination

Here is what it looks like in a button click handler

   Private Sub btnExecute_Click(sender As System.Object, e As System.EventArgs) Handles btnExecute.Click


      If TextBox1.Text.Length > 1 And TextBox1.Text.Contains(" ") Then

         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
         TextBox1.Clear()
         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 If

   End Sub
 
ok now thats handled i need to actually make it do that/output right? would i need to use something along these lines or no?

Function RemoveDuplicateWords(ByVal InputString As String) As String
 RemoveDuplicateWords = ""
 Try
 If InputString = Nothing Then
 RemoveDuplicateWords = ""
 Exit Function
 End If
 If InputString = "" Then
 RemoveDuplicateWords = ""
 Exit Function
 End If
 Dim d = New Dictionary(Of String, Boolean)
 Dim b As StringBuilder = New StringBuilder()
 Dim a As String() = InputString.Split(New Char() {" "c, ","c, "."c, vbLf, vbCr, "~"c})
 For Each current As String In a
 Dim lower As String = current.ToLower()
 If d.ContainsKey(lower) = False Then
 b.Append(current).Append(" ")
 d.Add(lower, True)
 End If
 Next
 RemoveDuplicateWords = b.ToString()
 End Try
 End Function

 
I'm afraid this is where the more experienced guys will have to step in as I'm just a rookie myself.

From what I can see, all this Function returns is the words to lowercase which can also be achieved by changing the line in the original code you submitted from:

TextBox1.Text &= sWord & " "


to

TextBox1.Text &= sWord.ToLower & " "
 
ok ty 22-degrees. well since i have the input part of it, how would you suggest that i try to make it actually output. i mean its being told to calculate the output with what you've shown me, but its not actually giving me any output.
 
That's strange.. Everytime i responded to you, I first checked the code to make sure it was doing what it was supposed to do..all i have is a form with a textbox and button.. "textbox1" and "btnExecute"

I typed in random words with some duplicates using capital letters and small, and it spat back out only non-duplicate letters in lower case..

Here is what I have and this works for me:

Public Class Form1

   Private Sub btnExecute_Click(sender As System.Object, e As System.EventArgs) Handles btnExecute.Click

      If TextBox1.Text.Length > 1 And TextBox1.Text.Contains(" ") Then

         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
         TextBox1.Clear()
         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 If


   End Sub


End Class


BedTime for me here so I hope you solve your problem soon.

Good Luck

PS, sorry to suggest this but are you clicking the button to make the code run???
 
22-degrees

Yes i was clicking the button, I figured out what it was that I was doing wrong. I had two buttons and two textboxes and only had the results outputting to to textbox1 instead of textbox2. quick question, do you know how to make things display alphabetically? if not thank you for all of your help and have a good night.
 
this is the code that i am working with:
Public Class

    
    '(Duplicate Word Removal) Write an application that inputs a sentence from the user (assume no punctuation),
    'then determines and displays the non-duplicate words in alphabetical order.  Treat upppercase and lowercase letters the same.
    '[Hint: you can use String method Split with no arguments, as in sentance.split(), to break a sentance into
    'an array of Strings containing the individual words.  By default, Split uses spaces as delimiters.  Use Strings
    'method ToLower in the Select and Order By clauses of your LINQ query to obtain the lower case version of each word.]
    Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text.Length > 0 And TextBox1.Text.Contains(" ") Then
            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
                    '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
            TextBox1.Clear()
            For Each sWord As String In sWords
                Array.Sort(sWords) 'sorts the words in the array alphabetically
                'if the word is something, then add the word to the text and add a space
                If sWord <> "" Then
                    TextBox2.Text &= sWord.ToLower & " "
                End If
            Next
        End If
    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 = ""
        TextBox2.Text = ""
    End Sub
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    End Sub
End Class





here is the problem:

when i enter in the text: i am small i am big i am tall i am bored
it spits out: i am big bored i small tall



it is not supposed to be spitting out the two i's
this code is supposed to remove the duplicate words and output the words (single) in alphabetical order. any ideas as to why this is happening?
 
Last edited:
your array.sort command is in the wrong place.. As i said, it needs to go BEFORE "For Each sWord As String In sWords".. so that it looks like this:

TextBox1.Clear()
Array.Sort(sWords) 'sorts the words in the array alphabeticallyFor Each sWord As String In sWords
 
Hi Guys,

Maybe this can help to simplify what you are trying to achieve. Have a read of the comments and as you will see it's much simpler.

VB.NET:
  Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    'here we split the string into an array and at the same time we convert to lower
    Dim strWords() As String = TextBox1.Text.ToLower.Split(" "c)
 
    'here I use a single linq statement to group the words for uniqueness, and then select the key of the group
    'and then return the results as a new array
    Dim strUniqueWords() As String = strWords.GroupBy(Function(x) x).Select(Function(x) x.Key).ToArray
 
    'here we sort the new array
    Array.Sort(strUniqueWords)
 
    'here we use string.join to convert the array back to a string and then display the results
    TextBox2.Text = String.Join(" ", strUniqueWords)
  End Sub
Hope that helps.

Cheers,

Ian
 
Back
Top