Golang program to find the duplicate characters in the string


In golang, identifying characters that appear more than once in a string is the first step in finding duplicate characters. To accomplish this, iterate through the string while keeping track of the characters that have already been encountered. Then, before adding a character, determine if it is already in the set of characters that have already been encountered. A character is a duplication if it already exists in the set. Let’s see its execution.

Method 1: Using map and a for loop

In this method, the program uses a map to keep track of the number of times each character appears in the string. It then iterates over the map and prints out any characters that have a count greater than 1.

Syntax

map

A map is a pre-built data structure in Golang that enables key-value pair storage and retrieval. A map's keys need to be distinct, and its values can be of any data type. Data may be efficiently stored and retrieved using maps, which are implemented as hash tables. The items can be added, retrieved, and removed using the [] operator, and they can be constructed using the make() method and the map keyword.

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable Example:s and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function create a string hello alexa and assign it to a variable named mystr.

  • Step 3 − To keep track of how many times each character appears in the string, initialise an empty map.

  • Step 4 − Use a for loop to repeatedly iterate over each character in the string.

  • Step 5 − Add one to the character count for each character on the map and utilize a for loop to iterate across the map.

  • Step 6 − Check to see if the count is more than 1 for each character on the map. Print out the character and its count if it is.

  • Step 7 − Once every character in the string has been examined and checked for duplication, repeat this process.

  • Step 8 − Print the duplicate characters on the screen using fmt.Println() function where ln means new line.

Example

In this example we will see how to find duplicate characters in the string using Map and for loop

package main
import (
   "fmt"
)

func main() {
   mystr := "hello alexa"    //create string
   fmt.Println("The original string given here is:", mystr)
   counts := make(map[rune]int)
   for _, char := range mystr {
      counts[char]++
   }
   fmt.Println("The duplicate characters in this string can be represented as:")
   for char, count := range counts {
      if count > 1 {
         fmt.Printf("%c appears %d times\n", char, count) //print the duplicate characters on the screen
      }
   }
}

Output

The original string given here is: hello alexa
The duplicate characters in this string can be represented as:
e appears 2 times
l appears 3 times
a appears 2 times

Method 2: Using count variable

In this method, the program keeps track of the frequency of each character in the input string using a map. Then, iterating through the string character by character, it determines whether there are more than one of that character's occurrences in the map. If it is, it is printed to the console as a duplicate character. Let’s see the Example: and the algorithm.

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable Example:s and fmt helps in formatting input and output.

  • Step 2 − Create a function main and in that function set up a variable input_value with the string you wish to search for duplicate characters in.

  • Step 3 − For the purpose of storing the frequency of each character in the input string, create an empty map called counts.

  • Step 4 − Use a for loop to repeatedly iterate over each character in the input string.

  • Step 5 − Verify that each character's count in the counts map is equal to 1. For each character. If so, print the character to the console because it is a duplication.

  • Step 6 − Add the character to the map for each iteration if it doesn't already exist, or increase the count by one.

  • Step 7 − If there are any duplicate characters in the input string, the output will be printed on the console using fmt.Println() function where ln means new line.

Example

In this example, we will find duplicate characters in the string using Count Variables

package main
import (
   "fmt"
)

func main() {
   input_value := "hello,alexa!"  //create string
   fmt.Println("The original string given here is:", input_value)
   counts := make(map[rune]int)
   for _, char := range input_value {
      if counts[char] == 1 {
         fmt.Printf("'%c' is a duplicate character.\n", char)
      }
      counts[char]++  //increment the count
   }
}

Output

The original string given here is: hello,alexa!
'l' is a duplicate character.
'e' is a duplicate character.
'a' is a duplicate character.

Conclusion

We executed the program of finding duplicate characters in the string using two examples. In the first example we used map and a for loop and in the second example we used count variable to keep count. Hence, program executed successfully.

Updated on: 20-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements