Re: What is Best Method To Determine Duplicate Items in a Large List?
Re: What is Best Method To Determine Duplicate Items in a Large List?
- Subject: Re: What is Best Method To Determine Duplicate Items in a Large List?
- From: "Nigel Garvey" <email@hidden>
- Date: Sun, 13 Aug 2017 10:25:44 +0100
Shane Stanley wrote on Sun, 13 Aug 2017 14:06:34 +1000:
>There might be a quicker way, but this should do the trick:
>
>use AppleScript version "2.4" -- Yosemite (10.10) or later
>use framework "Foundation"
>use scripting additions
>
>set theBigList to {"a", "b", "c", "d", "e", "f", "g", "h", "a", "i",
"e", "e"}
>set theBigList to current application's NSArray's
arrayWithArray:theBigList
>set theCount to theBigList's |count|()
>-- get duplicate items
>set theCountedSet to current application's NSCountedSet's
>setWithArray:theBigList
>theCountedSet's minusSet:(current application's NSSet's
>setWithSet:theCountedSet)
>set theDupes to theCountedSet's allObjects()
>-- get index of the dupes
>set indexInfo to {} -- to hold info
>set theStart to 0
>repeat with aDupe in theDupes
> set end of indexInfo to {aDupe as text}
> repeat
> set theIndex to (theBigList's indexOfObject:aDupe
>inRange:{theStart, theCount - theStart})
> if theIndex > theCount - 1 then -- not found, so move
to next
>dupe
> set theStart to 0
> exit repeat
> else if theIndex = theCount - 1 then -- last item, so
record
>and move to next dupe
> set end of item -1 of indexInfo to theIndex + 1
> set theStart to 0
> exit repeat
> else -- record and update start range
> set end of item -1 of indexInfo to theIndex + 1
> set theStart to theIndex + 1
> end if
> end repeat
>end repeat
>return indexInfo
>--> {{"e", 5, 11, 12}, {"a", 1, 9}}
This is essentially Shane's script, but simplified (and slightly faster)
in the repeats:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set theBigList to {"a", "b", "c", "d", "e", "f", "g", "h", "a", "i", "e", "e"}
set theBigList to current application's NSArray's arrayWithArray:theBigList
set theCount to theBigList's |count|()
-- get a counted set of the duplicate instances of any duplicated values
set countedDupes to current application's NSCountedSet's
setWithArray:theBigList
countedDupes's minusSet:(current application's NSSet's
setWithSet:countedDupes)
-- get the indices of the duplicated values' first and dupe instances
set duplicatedValues to countedDupes's allObjects()
set indexInfo to {}
repeat with thisValue in duplicatedValues
-- Value and first index.
set thisIndex to (theBigList's indexOfObject:(thisValue)) + 1
set thisInfo to {thisValue as text, thisIndex}
-- Indices of dupes.
repeat (countedDupes's countForObject:(thisValue)) times
set thisIndex to (theBigList's indexOfObject:(thisValue)
inRange:({thisIndex, theCount - thisIndex})) + 1
set end of thisInfo to thisIndex
end repeat
set end of indexInfo to thisInfo
end repeat
return indexInfo
--> {{"e", 5, 11, 12}, {"a", 1, 9}}
NG
_______________________________________________
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