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: Mon, 14 Aug 2017 11:58:58 +0100
Shane Stanley wrote on Mon, 14 Aug 2017 17:10:47 +1000:
>On 14 Aug 2017, at 1:36 pm, Jim Underwood <email@hidden> wrote:
>>
>> Is it possible to retain the order of the source list (theBigList) in
the
>> resulting dup list (indexInfo)?
>>
>> My source list is Note Titles that I have sorted in alphanumeric
order, but
>> the script seems to ignore this.
>
>The quickest would be to sort the duplicates …
An alternative would be to get an ordered set from the original array
and to filter it for values which occur in the counted duplicates. This
should ensure that the results are returned in the order the values
first occur in the original list, whatever that order may happen to be.
set duplicatedValues to current application's NSMutableOrderedSet's
orderedSetWithArray:theBigList
set predicateForBeingInCountedDupes to current application's NSPredicate's
predicateWithFormat:("self IN %@") argumentArray:({countedDupes})
duplicatedValues's filterUsingPredicate:(predicateForBeingInCountedDupes)
This version preserves the original order and works with several classes
of value:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set theBigList to {"a", "b", "z", "c", 17, "d", {a:"aardvark", b:"balloon"},
"e", "f", {b:"balloon", a:"aardvark"}, "g", "h", "a", 17, "i", "e", "z", "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 an ordered set of theBigList's values and filter it for those known to
be duplicated.
set duplicatedValues to current application's NSMutableOrderedSet's
orderedSetWithArray:theBigList
set predicateForBeingInCountedDupes to current application's NSPredicate's
predicateWithFormat:("self IN %@") argumentArray:({countedDupes})
duplicatedValues's filterUsingPredicate:(predicateForBeingInCountedDupes)
-- get the indices of the duplicated values' first and dupe instances
set indexInfo to {}
repeat with thisValue in duplicatedValues -- Going through the ordered set.
-- Value and first index.
set thisIndex to (theBigList's indexOfObject:(thisValue)) + 1
set thisInfo to (current application's NSArray's
arrayWithObjects_(thisValue, thisIndex)) as list
-- 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
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