A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Duplicate Character Counter in Swift page! Here, you'll find the source code for this program as well as a description of how the program works.
import Foundation
func error() {
print("Usage: please provide a string")
}
func duplicateCharacterCounter(string: [String]) {
if string.isEmpty || string[0].isEmpty {
error()
} else {
let input = string[0]
var dict = [Character: Int]()
for char in input{
dict[char, default: 0] += 1
}
var flag = false
for char in input{
if let count = dict[char], count > 1 {
flag = true
print("\(char): \(count)")
dict[char] = 0
}
}
if !flag {
print("No duplicate characters")
}
}
}
let string = CommandLine.arguments.dropFirst().map { String($0) }
duplicateCharacterCounter(string: Array(string))
Duplicate Character Counter in Swift was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.