FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
coding-Interview/MajorityElement.java at master · arjunmullick/coding-Interview · GitHub
arjunmullick
/
coding-Interview
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
coding-Interview
/
MajorityElement.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (28 loc) · 767 Bytes
Breadcrumbs
coding-Interview
/
MajorityElement.java
Copy path
File metadata and controls
35 lines (28 loc) · 767 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
com
.
leetcode
;
public
class
MajorityElement
{
//https://leetcode.com/problems/majority-element/
class
Solution
{
public
int
majorityElement
(
int
[]
nums
) {
int
count
=
0
;
int
result
=
nums
[
0
];
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++){
if
(
count
==
0
){
result
=
nums
[
i
];
}
if
(
nums
[
i
] ==
result
){
count
++;
}
else
{
count
--;
}
}
return
result
;
}
}
// https://www.cs.utexas.edu/~moore/best-ideas/mjrty/example.html#step13
/**With sort
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
**/
}
Back
|
FazBrowse Home
|
New Git URL