Results 1 to 4 of 4

Thread: Remove duplicates from List - Update

  1. #1
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 10
    Location
    Wales UK
    Posts
    540

    Remove duplicates from List - Update

    Hi just a quick example of removing duplicates from a list object, useful for strings or numbers.
    Comments and suggests welcome.

    Example

    vbnet Code:
    1. Public Class Form1
    2.  
    3.     Private Sub ListRemoveDups(ByVal Source As List(Of String), Optional ByVal MatchCase As Boolean = False)
    4.         'Sort the array
    5.         Source.Sort()
    6.  
    7.         'Do the duplicates remove.
    8.         For x As Integer = Source.Count - 1 To 1 Step -1
    9.             'Check if we want
    10.             If MatchCase Then
    11.                 'Do the check
    12.                 If Source(x).ToLower() = Source(x - 1).ToLower() Then
    13.                     Source.RemoveAt(x)
    14.                 End If
    15.             Else
    16.                 'Do the check
    17.                 If Source(x) = Source(x - 1) Then
    18.                     Source.RemoveAt(x)
    19.                 End If
    20.             End If
    21.         Next x
    22.     End Sub
    23.  
    24.     Private Sub cmdDemo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDemo.Click
    25.         'Remove duplicates from list
    26.         Dim names As New List(Of String)
    27.         'Add some names
    28.         names.Add("Ben")
    29.         names.Add("Ben")
    30.         names.Add("David")
    31.         names.Add("Chris")
    32.         names.Add("David")
    33.         names.Add("Ben")
    34.         names.Add("Chris")
    35.         names.Add("david")
    36.  
    37.         'Match Case
    38.         ListRemoveDups(names, True)
    39.  
    40.         'Display names
    41.         For x As Integer = 0 To names.Count - 1
    42.             MessageBox.Show(names(x), "Demo", MessageBoxButtons.OK, MessageBoxIcon.Information)
    43.             'Return Ben,Chris,david
    44.         Next x
    45.  
    46.     End Sub
    47. End Class

  2. #2
    Frenzied Member
    Join Date
    Jul 06
    Location
    MI
    Posts
    1,520

    Re: Remove duplicates from List

    You could also use LINQ for this. Something like:

    Code:
            Dim uniqueNames = From u In names Distinct
    
            For Each n In uniqueNames
                Console.WriteLine(n)
            Next

  3. #3
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 10
    Location
    Wales UK
    Posts
    540

    Re: Remove duplicates from List

    Hello again this is an update of the code I submitted this morning. Anyway I now made it into a small little program.
    You can give it a source and remove duplicates that is sorted or unsorted
    You can also copy or save your output. Hope you like it.

    Comments and suggests welcome.

    Attached Files Attached Files
    Last edited by BenJones; Jul 6th, 2012 at 03:37 PM.

  4. #4
    Member
    Join Date
    Apr 09
    Posts
    55

    Re: Remove duplicates from List - Update

    When performing such tedious operations I like to rely on LINQ rather than Loops. You should really look into LINQ, its quite powerful

    Code:
    'remove duplicates
    Dim cleanList As List(Of String) = IO.File.ReadAllLines("[FILENAME]").Distinct.ToList
    
    'remove duplicates + null lines
    Dim cleanList As List(Of String) = IO.File.ReadAllLines("[FILENAME]").Where(Function(line) Not line.Equals(String.Empty)).Distinct.ToList

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •