What's the best way to find duplicates in a NSArray?

I can’t remember where in the Apple Reference Documentation I have seen a most direct method to achieve this:

use framework "Foundation"
use scripting additions

set arrayA to current application's NSArray's arrayWithArray:{1, 2, 3, 4, 3, 5, 3}
set arrayB to (current application's NSOrderedSet's orderedSetWithArray:arrayA)'s allObjects()
set theDiffs to (arrayB's differenceFromArray:arrayA)'s removals()
if theDiffs as list = {} then return {}
return (theDiffs's valuesForKeys:{"object", "index"}) as record
1 Like