using
System;
using
System.Collections.Generic;
using
System.Linq;
public
class
Pair
{
public
int
first;
public
int
second;
}
public
class
GFG
{
public
static
void
top3Repeated(
int
[] arr,
int
n)
{
if
(n < 3)
{
Console.Write(
"Invalid Input"
);
return
;
}
var
freq =
new
Dictionary<
int
,
int
>();
for
(
int
i = 0; i < n; i++)
{
if
(freq.ContainsKey(arr[i]))
{
freq[arr[i]] = 1 + freq[arr[i]];
}
else
{
freq[arr[i]] = 1;
}
}
var
x =
new
Pair();
var
y =
new
Pair();
var
z =
new
Pair();
x.first = y.first = z.first =
int
.MinValue;
foreach
(KeyValuePair<
int
,
int
> curr
in
freq)
{
if
(
int
.Parse(curr.Value.ToString()) > x.first)
{
z.first = y.first;
z.second = y.second;
y.first = x.first;
y.second = x.second;
x.first =
int
.Parse(curr.Value.ToString());
x.second =
int
.Parse(curr.Key.ToString());
}
else
if
(
int
.Parse(curr.Value.ToString()) > y.first)
{
z.first = y.first;
z.second = y.second;
y.first =
int
.Parse(curr.Value.ToString());
y.second =
int
.Parse(curr.Key.ToString());
}
else
if
(
int
.Parse(curr.Value.ToString()) > z.first)
{
z.first =
int
.Parse(curr.Value.ToString());
z.second =
int
.Parse(curr.Key.ToString());
}
}
Console.Write(
"Three largest elements are "
+ x.second.ToString() +
" "
+ y.second.ToString() +
" "
+ z.second.ToString());
}
public
static
void
Main(String[] args)
{
int
[] arr = {3, 4, 2, 3, 16, 3, 15, 16, 15, 15, 16, 2, 3};
var
n = arr.Length;
GFG.top3Repeated(arr, n);
}
}