Simpliest NSOrderedSet alternative on linux for a simple Swift-Script?

I have found that the NSOrderedSet used in my Swift script (one-file only) is not deduplicating things at all on Linux. It works fine on macOS. Surprisingly, Swift 5.5 on Linux doesn't throw an error for this, either.

However, it is not an ideal choice to use a giant Swift-Collections package with a one-file-only Swift script.

Is there any other choice of ordered deduplicators similar to NSOrderedSet?

Yes, swift-collections has a Swift-native OrderedSet that would be perfect for your use-case.

1 Like

Um. It's an ideal choice for at least Xcode-managed project.

I doubt the troubles of using it in a single Swift script file, still.

If copying the source of OrderedSet into your script is unacceptable, there is no acceptable alternative. Unfortunately scripts can't import packages and there is no other built in OrderedSet alternative.


If you just need a one-time depublication of an array, you can write something like this:

extension RangeReplaceableCollection where Element: Hashable {
  mutating func removeDuplicates() {
    var alreadySeen: Set<Element> = []
    removeAll { !alreadySeen.insert($0).inserted }

Thank you, comrade. This is a miracle.