Duplicate Character Counter in Swift

Published on 12 November 2024 (Updated: 12 November 2024)

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.

Current Solution

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.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.