Write a program to find out duplicate characters in a string.

Write a program to find out duplicate or repeated characters in a string, and calculate the count of repeatation.

  • Convert the string to all lower case first
  • Create HashMap with <Character,Integer>
  • Iterate the String char by char and check its present in hashmap or not
    If not present put ..char and 1
    If Present get the existing value and put….char and value+1


Kotlin program to find out duplicate characters in a string

import java.util.HashMap

fun main(a: Array<String>) {
    var s = "aabbccA"
    s = s.toLowerCase()
    val map = HashMap()
    for (i in s.toCharArray()) {
        if (map.keys.contains(i)) {
            var x = map[i]
            map[i] = 1+x!!
        } else {
            map[i] = 1
        }

    }
    for (c in map.keys) {
        println(c + "  " + map[c])
    }
}


Output


a  3
b  2
c  2

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions