Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

remove duplicate elements from a Generic list

Posted on 2011-03-14
2
Medium Priority
?
274 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 400 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 100 total points
ID: 35138753
Why not use a dictionary or use linq's distinct function?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 …
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 …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

721 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