Solved

remove duplicate elements from a Generic list

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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