-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0169-Majority-element.cs
More file actions
37 lines (31 loc) · 915 Bytes
/
0169-Majority-element.cs
File metadata and controls
37 lines (31 loc) · 915 Bytes
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
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0169.Majority_element
{
public class _0169_Majority_element
{
public int MajorityElement(int[] nums)
{
Dictionary<int, int> dic = new Dictionary<int, int>();
foreach (int num in nums)
{
if (dic.ContainsKey(num))
dic[num]++;
else
dic.Add(num, 1);
if (dic[num] > nums.Length / 2)
return num;
}
//for (int i = 0; i < nums.Length; i++)
//{
// if (dic.ContainsKey(nums[i]))
// dic[nums[i]]++;
// else
// dic.Add(nums[i], 1);
// if (dic[nums[i]] > nums.Length / 2) return nums[i];
//}
return -1;
}
}
}