Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Sort characters by frequency in Go (Golang)

Posted on July 28, 2023July 28, 2023 by admin

Table of Contents

  • Overview
  • Program

Overview

An input string is given. The objective is to sort the string based on frequency. We have to sort based on decreasing order of the frequency of characters. Let’s understand it with an example

Example 1

Input: "bcabcb"
Output: "bbbcca"

Example 2

Input: "mniff"
Output: "ffmni"

Program

Below is the program for the same

package main
import (
"fmt"
"sort"
)
func frequencySort(s string) string {
stringMap := make(map[byte]int)
lenS := len(s)
for i := 0; i < lenS; i++ {
stringMap[s[i]]++
}
itemArray := make([]item, 0)
for key, value := range stringMap {
i := item{
char: key,
frequency: value,
}
itemArray = append(itemArray, i)
}
sort.Slice(itemArray, func(i, j int) bool {
return itemArray[i].frequency > itemArray[j].frequency
})
output := ""
for i := 0; i < len(itemArray); i++ {
for j := 0; j < itemArray[i].frequency; j++ {
output = output + string(itemArray[i].char)
}
}
return output
}
type item struct {
char byte
frequency int
}
func main() {
output := frequencySort("bcabcb")
fmt.Println(output)
output = frequencySort("mniff")
fmt.Println(output)
}

Output:

bbbcca
ffmni

Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang - Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you - All Design Patterns Golang

Also, check out our system design tutorial series here - System Design Tutorial Series

  • go
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP