Group all occurrences of characters according to first appearance | C # program to print all occurrences of every character together . ; Since only lower case characters are there ; Method to print the string ; Initialize counts of all characters as 0 ; Count occurrences of all characters in string ; Starts traversing the string ; Print the character till its count in hash array ; Make this character 's   count value as 0. ; Driver code
using System ; class GFG { static int MAX_CHAR = 26 ; static void printGrouped ( String str ) { int n = str . Length ; int [ ] count = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) count [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) { while ( count [ str [ i ] - ' a ' ] != 0 ) { Console . Write ( str [ i ] ) ; count [ str [ i ] - ' a ' ] -- ; } count [ str [ i ] - ' a ' ] = 0 ; } } public static void Main ( ) { string str = " geeksforgeeks " ; printGrouped ( str ) ; } }