16

I have specific logic in Java that uses HashSet<String>. Set collection contains only unique items.

For example:

Set<String> mySets = new HashSet<String>();
mySets.add("a");
mySets.add("a");
mySets.add("b");
mySets.add("a");

I get: ["a","b"].

What is equivalent collection in Swift?

Thanks,

snaggs
  • 5,543
  • 17
  • 64
  • 127
  • this page https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html talks about arrays and dictionary. Since Dictionary keys are unique, you could use a dummy value as value and work with keys only (like current HashSet implementation of Java) – Marco Acierno Aug 11 '14 at 15:14
  • Swift 1.2 has a native Set type now, compare http://stackoverflow.com/a/28426765/1187415. – Martin R Feb 10 '15 at 08:27
  • @MartinR thank you, wait for release – snaggs Feb 10 '15 at 11:48

1 Answers1

30

The Swift to Java's HashSet is Set.

Example:

var s = Set<Character>()
s.insert("a")
s.insert("a")
s.insert("b")
s.insert("a")
print("s: \(s)")

output:

s: ["b", "a"]

Official docs

Mastergalen
  • 4,289
  • 3
  • 31
  • 35
zaph
  • 111,848
  • 21
  • 189
  • 228