using
System;
using
System.Collections;
using
System.Collections.Generic;
class
GFG
{
static
List<pair> findMostFrequent(List<List<
char
>> arr,
int
n)
{
Dictionary<
char
,
int
> unmap =
new
Dictionary<
char
,
int
>();
for
(
int
i = 0 ; i < n ; i++) {
for
(
int
j = 0 ; j < n ; j++) {
if
(!unmap.ContainsKey(arr[i][j])){
unmap.Add(arr[i][j], 0);
}
unmap[arr[i][j]]++;
}
}
List<pair> arr2 =
new
List<pair>();
foreach
(KeyValuePair<
char
,
int
> i
in
unmap) {
arr2.Add(
new
pair(i.Value, i.Key));
}
arr2.Sort(
new
Comp());
return
arr2;
}
public
static
void
Main(
string
[] args){
int
N = 3;
List<List<
char
>> arr =
new
List<List<
char
>>{
new
List<
char
>{
'a'
,
'a'
,
'a'
},
new
List<
char
>{
'b'
,
'a'
,
'c'
},
new
List<
char
>{
'd'
,
'c'
,
'a'
}
};
List<pair> ans = findMostFrequent(arr, N);
for
(
int
i = 0 ; i < ans.Count ; i++) {
Console.WriteLine(ans[i].second +
" : "
+ ans[i].first);
}
}
}
public
class
pair{
public
int
first;
public
char
second;
public
pair(
int
first,
char
second){
this
.first = first;
this
.second = second;
}
}
class
Comp : IComparer<pair>{
public
int
Compare(pair o2,pair o1){
if
(o1.first == o2.first){
return
o1.second - o2.second;
}
return
o1.first - o2.first;
}
}