Swift Programming: Returns true if the string has only unique characters

How to efficiently verify if string has only unique characters

Arc Sosangyo
3 min readJun 4, 2024

Chances are, you may encounter this kind of issue either in your professional tasks or during a coding examination.

Create a function that returns true if the string has only unique characters. We’ll consider the case sensitivity of letters. Here’s a sample input and its corresponding output.

  • “Swift” returns true.
  • “swiftSWIFT” returns true since case sensitivity will take into account as mentioned earlier.
  • “Swift Programming” returns false since multiple letters appeared twice in the string such as letter i, r, g, and m.

If you’re like me, who cannot figure this out in one go, then it’s better that we break this down to make it easier. Let’s start by doing the easiest part: creating a function that accepts a string parameter and returns a Bool.

func checkIfUniqueCharacters(_ str: String) -> Bool {


To tackle the main issue, we can convert the input string into a collection of individual characters, like an array. This allows us to efficiently check for duplicates. However, since the problem mentions “unique” characters, a Set is the ideal…



Arc Sosangyo

Father | Husband | Self-help Author | Coder | Traveler