Solved

remove duplicate elements from a Generic list

Posted on 2011-03-14
2
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
UDP Data Capture Thread Issue 7 44
Broken .resx file generating errors 18 44
Help with viewing image in ReportViewer 14 32
VB .net 2010 Byte array 2 24
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…

752 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