Skip to content

Commit c4e3493

Browse files
committed
RemoveDuplicatesfromSortedArray
1 parent e76268d commit c4e3493

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* ==============================================================================
2+
* 功能描述:ThirdMaximumNumber
3+
* 创 建 者:gz
4+
* 创建日期:2017/4/21 8:25:57
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
//Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
11+
12+
//Example 1:
13+
//Input: [3, 2, 1]
14+
15+
//Output: 1
16+
17+
//Explanation: The third maximum is 1.
18+
//Example 2:
19+
//Input: [1, 2]
20+
21+
//Output: 2
22+
23+
//Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
24+
//Example 3:
25+
//Input: [2, 2, 3, 1]
26+
27+
//Output: 1
28+
29+
//Explanation: Note that the third maximum here means the third maximum distinct number.
30+
//Both numbers with value 2 are both considered as second maximum.
31+
namespace Array.Lib
32+
{
33+
/// <summary>
34+
/// #414 : Third Maximum Number
35+
/// </summary>
36+
public class ThirdMaximumNumber
37+
{
38+
public int GetThirdMax(int[] a)
39+
{
40+
int first = a.Max();
41+
int[] a2 = a.Where(r => r != first).ToArray();
42+
if (a2.Length == 0)
43+
return first;
44+
int second = a2.Max();
45+
int[] a3 = a2.Where(r => r != second).ToArray();
46+
if (a3.Length == 0)
47+
return first;
48+
return a3.Max();
49+
}
50+
51+
}
52+
}

0 commit comments

Comments
 (0)