Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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 ajoutée par Abhishek Chatterjee , Technical Specialist , Philips Innovation
Date de publication: 2017/12/20
Abhishek Chatterjee
par 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

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?