none
Removing Duplicates

    Question

  • This seems like a simple question, How do i remove duplicates from a string collection?
    Friday, April 23, 2010 7:09 AM

Answers

  •  

    I'm not exactly sure how to apply that to my situation . I have a setting in my project that records a web browser history , but when a user looks at the history list it displays a lot of duplicate entry's. So i need to figure out how to remove the duplicate entry's from the string collection programatically so the user dosen't see the duplicates.

    Hi Steve82291 ,

     

    You could use a LIST or / and an array, each of type STRING.

    It is probably easier to use a LIST as you can simply use the ADD method to add a NEW string to a LIST.

    However, if you also want the LIST in an array that is easy to do as well.

     

    What you could do, if you are using a ComboBox for example for use in a WebBrowser is add the STRING

    only if the collection does NOT Contain the STRING already.  :-)    :-D

     

    Translating "does Not Contain " into Vb.Net is the key here.

     

    With one WebBrowser and one ComboBox on a Form try this code please.

    The code below is pretty much self-explanatory. :-)

     

     

    Public Class Form1
    
     Dim stringCollectionAsAnArray() As String
     Dim stringCollectionAs_aList As New List(Of String)
    
     Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    
      If Not (stringCollectionAs_aList.Contains(ComboBox1.Text)) Then
    
       stringCollectionAs_aList.Add(ComboBox1.Text)
       ComboBox1.Items.Add(ComboBox1.Text)
    
      End If
    
      'To copy a LIST into an array use .ToArray >>
      stringCollectionAsAnArray = stringCollectionAs_aList.ToArray
    
      'To copy an array into a List use .ToList >>
      stringCollectionAs_aList = stringCollectionAsAnArray.ToList
    
     End Sub
    
     Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
    
      If e.KeyCode = Keys.Enter Then
       If System.Uri.IsWellFormedUriString(ComboBox1.Text, UriKind.RelativeOrAbsolute) Then
        WebBrowser1.Navigate(ComboBox1.Text)
       End If
      End If
    
     End Sub
    
     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    
      If System.Uri.IsWellFormedUriString(ComboBox1.SelectedItem.ToString, UriKind.RelativeOrAbsolute) Then
       WebBrowser1.Navigate(ComboBox1.SelectedItem.ToString)
      End If
    
     End Sub
    End Class

     

    If you do not want every sub-page of a website recorded too then let us know.

    Say for example you only want one http://www.microsoft.com/en/us/default.aspx

    then please say so. :-)

     

    Regards,

    John



    Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4
    • Proposed as answer by Cor LigthertMVP Friday, April 23, 2010 4:21 PM
    • Marked as answer by Steve82291 Friday, April 23, 2010 5:08 PM
    Friday, April 23, 2010 4:03 PM

All replies

  •     Dim myStrings As New List(Of String) From {"Value1", "Value2", "Value1", "Value3", "Value4", "Value4"}
    
        myStrings = myStrings.Distinct().ToList() '<< All that you need, the rest is for example
    That work for you?
    Friday, April 23, 2010 7:30 AM
  • I'm not exactly sure how to apply that to my situation. I have a setting in my project that records a web browser history, but when a user looks at the history list it displays a lot of duplicate entry's. So i need to figure out how to remove the duplicate entry's from the string collection programatically so the user dosen't see the duplicates.
    Friday, April 23, 2010 7:47 AM
  • I have a setting in my project that records a web browser history, but when a user looks at the history list it displays a lot of duplicate entry's.
    Post this relevant code to receive accurate help.

    v/r LikeToCode....Mark the best replies as answers.
    Friday, April 23, 2010 1:27 PM
  • I'm not exactly sure how to apply that to my situation. I have a setting in my project that records a web browser history, but when a user looks at the history list it displays a lot of duplicate entry's. So i need to figure out how to remove the duplicate entry's from the string collection programatically so the user dosen't see the duplicates.


    How did your user view the history? Is it from listbox or combobox. You can check if any of these control contains the item before add it. Or you can remove the duplicate from string collection before adding it to the control.

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Friday, April 23, 2010 2:52 PM
  •  

    I'm not exactly sure how to apply that to my situation . I have a setting in my project that records a web browser history , but when a user looks at the history list it displays a lot of duplicate entry's. So i need to figure out how to remove the duplicate entry's from the string collection programatically so the user dosen't see the duplicates.

    Hi Steve82291 ,

     

    You could use a LIST or / and an array, each of type STRING.

    It is probably easier to use a LIST as you can simply use the ADD method to add a NEW string to a LIST.

    However, if you also want the LIST in an array that is easy to do as well.

     

    What you could do, if you are using a ComboBox for example for use in a WebBrowser is add the STRING

    only if the collection does NOT Contain the STRING already.  :-)    :-D

     

    Translating "does Not Contain " into Vb.Net is the key here.

     

    With one WebBrowser and one ComboBox on a Form try this code please.

    The code below is pretty much self-explanatory. :-)

     

     

    Public Class Form1
    
     Dim stringCollectionAsAnArray() As String
     Dim stringCollectionAs_aList As New List(Of String)
    
     Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    
      If Not (stringCollectionAs_aList.Contains(ComboBox1.Text)) Then
    
       stringCollectionAs_aList.Add(ComboBox1.Text)
       ComboBox1.Items.Add(ComboBox1.Text)
    
      End If
    
      'To copy a LIST into an array use .ToArray >>
      stringCollectionAsAnArray = stringCollectionAs_aList.ToArray
    
      'To copy an array into a List use .ToList >>
      stringCollectionAs_aList = stringCollectionAsAnArray.ToList
    
     End Sub
    
     Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
    
      If e.KeyCode = Keys.Enter Then
       If System.Uri.IsWellFormedUriString(ComboBox1.Text, UriKind.RelativeOrAbsolute) Then
        WebBrowser1.Navigate(ComboBox1.Text)
       End If
      End If
    
     End Sub
    
     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    
      If System.Uri.IsWellFormedUriString(ComboBox1.SelectedItem.ToString, UriKind.RelativeOrAbsolute) Then
       WebBrowser1.Navigate(ComboBox1.SelectedItem.ToString)
      End If
    
     End Sub
    End Class

     

    If you do not want every sub-page of a website recorded too then let us know.

    Say for example you only want one http://www.microsoft.com/en/us/default.aspx

    then please say so. :-)

     

    Regards,

    John



    Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4
    • Proposed as answer by Cor LigthertMVP Friday, April 23, 2010 4:21 PM
    • Marked as answer by Steve82291 Friday, April 23, 2010 5:08 PM
    Friday, April 23, 2010 4:03 PM
  • John,

    You sometimes let an answer go in the details which you give.

    You could use a LIST or / and an array, each of type STRING.

    It is probably easier to use a LIST as you can simply use the ADD method to add a NEW string to a LIST.

    However, if you also want the LIST in an array that is easy to do as well.

    That Italic part is why I proposed your reply as the answer

    It should not always be short like that, but don't overdo it!

    :-) 


    Success
    Cor
    Friday, April 23, 2010 4:23 PM
  • Hi Cor,

    Okay, I get the message, I think, but I only tend to go into lots of detail with new forum members. :-)

    I have not had many complaints so far with my approach to these MSDN forums.

    I think most forum members can also see the funny side of my over-emphasis most of the time. :-D

     

    Regards,

    John

     


    Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4
    Friday, April 23, 2010 4:48 PM
  • I feel John's way of explaining is good actually. Atleast for new members, summing up everything that S/He might ask again in just one reply instead of replying again and again 4-5 times saves time of both ends.

    even if its asked by experienced users, those who come to the thread later through search results would get whole reply at once.


    Thanks

    "Feel the Force !"  | My blog
    Friday, April 23, 2010 4:57 PM
  • I feel John's way of explaining is good actually . At least for new members, summing up everything that S/He might ask again in just one reply instead of replying again and again 4-5 times saves time of both ends.

    even if its asked by experienced users, those who come to the thread later through search results would get whole reply at once.


    Thanks

    "Feel the Force !"  | My blog


    Hi Omie,

    Thanks for your support. :-)    ;-]    :-D    :-)    ;-]    :-D    :-)    ;-]    :-D    :-)    ;-]    :-D    :-)    ;-]    :-D    :-)    ;-]    :-D

     

    Regards,

    John

     

     


    Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4
    Friday, April 23, 2010 5:06 PM
  • Thank you John,

    Your answer worked for me.

    Friday, April 23, 2010 5:11 PM
  • Hi John,

    Positively meant of course.

    It is your style, keep it, but don't overdo it.

    :-)

     


    Success
    Cor
    Friday, April 23, 2010 5:24 PM
  • Hi Steve,

    Your welcome. :-)

    By the way Steve, I wish I was as young as you look and just starting out in .Net programming.

    To Cor,

    No worries. ;-)

    I can't help myself!! Only kidding!!    :-P

     

    Regards,

    John

     


    Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4
    Friday, April 23, 2010 5:28 PM