Solved

remove duplicate elements from a Generic list

Posted on 2011-03-14
2
267 Views
Last Modified: 2012-05-11
Hello,
  I have function that should query a directory and return files that match a pattern. However, the list contain duplicate elements. How do I remove duplicate element from list<string>

e,g searchPattern = cap|cap.gz
file = f1.cap, f2.cap.gz
* The code below will add the files twice

Public Function GetFilesByExtension(ByVal dir As String, ByVal searchPattern As String) As String()
    Dim filelist As New List(Of String)
    Dim m_arExt() As String = Split(searchPattern, "|")
     For Each FileExt As String In m_arExt
            filelist.AddRange((IO.Directory.GetFiles(dir, FileExt)))
        Next
return Return filelist.ToArray
0
Comment
Question by:SiemensSEN
2 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 100 total points
ID: 35134388
It seems like the simplest solution would be to avoid adding the duplicates in the first place.
For Each FileExt As String In m_arExt
            For Each file As String In IO.Directory.GetFiles(Dir, FileExt)
                If Not filelist.Contains(file) Then
                    filelist.Add(file)
                End If
            Next
        Next

Open in new window

0
 
LVL 15

Assisted Solution

by:Russell_Venable
Russell_Venable earned 25 total points
ID: 35138753
Why not use a dictionary or use linq's distinct function?
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now