Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Write a program in Swift to find the first non-repeating character in a given string?

Looping over string characters only once

user-image
Question added by Abhishek Chatterjee , Technical Specialist , Philips Innovation
Date Posted: 2017/12/20
Abhishek Chatterjee
by Abhishek Chatterjee , Technical Specialist , Philips Innovation

func firstNotRepeatingCharacter(s: String) -> Character {

    var occurrences: [Character:Int] = [:]

    s.forEach{ occurrences[$0] = (occurrences[$0] ?? 0) + 1}

    return s.first{ occurrences[$0] == 1 } ?? "_"

}

More Questions Like This