realbasic-nug
[Top] [All Lists]

Re: Removing Duplicates in an Array

To: REALbasic-NUG <realbasic-nug@lists.realsoftware.com>
Subject: Re: Removing Duplicates in an Array
From: Kem Tekinay <ktekinay@mactechnologies.com>
Date: Sat, 31 Jan 2009 20:01:29 -0500
Authentication-results: mx.google.com; spf=neutral (google.com: 74.124.194.228 is neither permitted nor denied by best guess record for domain of realbasic-nug-bounces@lists.realsoftware.com) smtp.mail=realbasic-nug-bounces@lists.realsoftware.com
Delivered-to: listarchive@realsoftware.com
In-reply-to: <EAEE2D13-3E0E-4522-8676-707C77E654F2@declareSub.com>
Reply-to: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Sender: realbasic-nug-bounces@lists.realsoftware.com
Thread-index: AcmECJ1G9GHe/fotJkaLwj3uTa7vdg==
Thread-topic: Removing Duplicates in an Array
User-agent: Microsoft-Entourage/12.15.0.081119
On 1/31/09 3:45 PM, "Charles Yeomans" <charles@declareSub.com> wrote:

> Someone else suggested using a Dictionary object.  I suggest using it
> as follows.
> 
> Function RemoveDuplicates(theList() as Integer) as Integer()
>    dim d as new Dictionary
>    dim newList() as Integer
>    for i as Integer = 0 to UBound(theList)
>      if not d.HasKey(theList(i)) then
>        newList.Append theList(i)
>        d.Value(theList(i) = nil
>      end if
>    next
>    return newList
> End Function

It probably would be faster to predimension the array, especially for large
arrays. (And, this is supposed to be for strings.)

Function RemoveDuplicates(theList() as String) as String()
  dim d as new Dictionary
  dim lastIndex as Integer = theList.Ubound
  dim newList( -1 ) as string
  redim newList( lastIndex )
  dim newIndex as Integer = 0
  for each thisItem as String in theList
    if d.HasKey( thisItem ) then
      lastIndex = lastIndex - 1
    else
      newList( newIndex ) = thisItem
      newIndex = newIndex + 1
      d.Value( thisItem ) = nil
    end if
  next
  redim newList( lastIndex )
  return newList
End Function

This has been tested.

__________________________________________________________________________
Kem Tekinay                                                 (212) 201-1465
MacTechnologies Consulting                              Fax (914) 242-7294
http://www.mactechnologies.com                        Pager (917) 491-5546

  To join the MacTechnologies Consulting mailing list, send an e-mail to:
           mactechnologies_consulting-subscribe@yahoogroups.com









_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>


<Prev in Thread] Current Thread [Next in Thread>