Solved

Remove duplicates from array

Posted on 2006-10-26
4
246 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
[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
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

Industry Leaders: 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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

724 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