Solved

remove duplicate elements from a Generic list

Posted on 2011-03-14
2
270 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…

839 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