• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Eliminating duplicate items from a list
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Eliminating duplicate items from a list


  • Subject: Re: Eliminating duplicate items from a list
  • From: "Stockly, Ed" <email@hidden>
  • Date: Wed, 05 Sep 2007 16:05:10 -0700
  • Thread-topic: Eliminating duplicate items from a list

  First, there is nothing wrong with doing it this way...

set newList to {}
  repeat until originalList = {}
    set firstItem to (first item of originalList)
    set originalList to (rest of originalList)
    if firstItem is not in originalList then set newList to newList &
{firstItem}
   end repeat
return newList

Except, it takes longer if you're working with lists that contain 100's of
items.

I did some testing of various methods suggested and was surprised by some of
the results.
>
>NG> I'm sure the OSAX suggestion's much faster though.  :)
>
 Actually, no, on large lists the OSAX solution performed significantly
faster than some, but using script objects performed fastest.

On shorter lists, the all finished in 1 second or less.

Here's the script I tested with:

If you build the starting list with x repeating 400 times and y 100 times,
you'll notice the speed difference between A and D, but comment out B and C
before doing so.



property startingList : {}
set startingList to {}

repeat with x from 1 to 100
    repeat with y from 1 to 10
        set the end of startingList to y
    end repeat
end repeat
-------
set aStart to current date
set newList to AuniqueList()
set aEnd to current date
set aTime to aEnd - aStart

set bStart to current date
set newList to BuniqueList()
set bEnd to current date
set bTime to bEnd - bStart

set cStart to current date
set newList to CuniqueList()
set cEnd to current date
set cTime to cEnd - cStart

set dStart to current date
set newList to DuniqueList()
set dEnd to current date
set dTime to dEnd - dStart

set AppleScript's text item delimiters to " "
return {"List Size:", count of startingList, "Duplicates Found:", count of
newList, "A-time:", aTime, "B-time:", bTime, "C-time:", cTime, "D-time:",
dTime} as string
on AuniqueList()
    set uniqueList to {}
    script scriptObject
        property oList : startingList
        property ulist : uniqueList
    end script
    repeat with x from 1 to (count scriptObject's oList)
        set origItem to item x of scriptObject's oList
        if ({origItem} is not in scriptObject's ulist) then
            set end of scriptObject's ulist to origItem
        end if
    end repeat

    return scriptObject's ulist
end AuniqueList


on BuniqueList()
    set uniqueList to {}
    repeat with origItemRef in startingList
        set origItem to contents of origItemRef
        if uniqueList does not contain origItem then
            set end of uniqueList to origItem
        end if
    end repeat
end BuniqueList


on CuniqueList()
    set uniqueList to {}
    repeat with x from 1 to count of startingList
        copy item x of startingList to testItem
        if testItem is not in uniqueList then
            set the end of uniqueList to testItem
        end if
    end repeat
    return uniqueList
end CuniqueList

on DuniqueList()
    set uniqueList to union of startingList and startingList with removing
duplicates
    return uniqueList
end DuniqueList
------------
ES
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Prev by Date: Re: FileMaker AppleScript question...
  • Next by Date: Re: Eliminating duplicate items from a list
  • Previous by thread: Re: Eliminating duplicate items from a list
  • Next by thread: Re: Eliminating duplicate items from a list
  • Index(es):
    • Date
    • Thread