-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindmajority.cs
42 lines (34 loc) · 945 Bytes
/
findmajority.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections;
using System.Collections.Generic;
// To execute C#, please define "static void Main" on a class
// named Solution.
class Solution
{
static void Main(string[] args)
{
int[] arr = new int[9] {3, 3, 4, 2, 4, 4, 2, 4, 4};
findMajority(arr);
}
public static void findMajority(int[] arr)
{
int length = arr.Length;
Dictionary<int, int> map = new Dictionary<int, int>();
for(int i=0; i< length; i++)
{
if(map.ContainsKey(arr[i]))
{
int val = map[arr[i]];
map[arr[i]] = val + 1;
if(val + 1 > length /2)
{
Console.WriteLine(arr[i] + " ");
}
}
else
{
map.Add(arr[i], 1);
}
}
}
}