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
Solved

Remove duplicates from array

Posted on 2006-10-26
4
244 Views
Last Modified: 2010-04-30
I have a program that sends out emails to a generated list separated by semi-colons.  There are never more than 5 addresses but I want to be sure that I don't have any duplicates.  I can load them into an array using split(emails, ";").  How do I sort and remove duplicates?

Thanks,

Traigo
0
Comment
Question by:traigo
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17817264
from the array I would add them to a collection;


dim email as string
dim emails() as string
dim col as collection
dim l as long

email = "a@email.com;b@email.com;a@email.com;c@email.com"
emails = split(email, ";")

set col = new collection
on error resume next
for l = lbound(emails) to ubound(emails)
  email = emails(l)
   'as we have error resume next active, this will ensure that the code continues, although we might try to add a duplicate key here
  col.add email, email
next


here you have a collection of unique email addresses

0
 
LVL 35

Accepted Solution

by:
Raynard7 earned 500 total points
ID: 17817265
Hi,
I'd use a dictionary (from the microsoft scripting runtime libary)

dictionaries have an exists property for their keys

for a new dictionary I would use

dim dicCheck as new dictionary

I'd split the emails into an array - then foreach element in the array check to see if it is in the array - and if it is then do not add it again

ie

if dicCheck.exists(arrayElement) = false then
    dicCheck.Add arrayElement, 1
end if

then to put everything back out i would do foreach element in dicCheck.Keys and output this into one string
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 17817311
Maybe try something like:

dim emails() as string
dim emailList as string
dim i as long, j as long
dim dupe as boolean

emailList = "a@email.com;b@email.com;a@email.com;c@email.com"
emails = split(emailList,";")
do
dupe = false
for i = lbound(emails) to ubound(emails)
 for j = lbound(emails) to ubound(emails)
  if ((emails(i) = emails(j) )and (i <> j)) then
   emails(i) = ""
   dupe = true
   exit for
 end if
 next j
next i
loop while (dupe)
0
 

Author Comment

by:traigo
ID: 17817338
Thanks for the quick response.  I got it working with raynard7's suggestion.

Dim Dict As Dictionary
Set Dict = New Dictionary
arr = split("me@domain.com;me@domain.com;you@domain.com;me@domain.com", ";")

dim i as long

for i=0 to ubound(arr)
      if dict.exists(arr(i)) = false Then
            dict.add arr(i), 1
      End If
next

dim elem as variant
dim str as string
str = ""
for each elem in dict.keys
str = str & elem & ";"
next

str = Left(str, len(str) - 1)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

860 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