Skip to content

Commit d336421

Browse files
add 169
1 parent 62106d7 commit d336421

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,25 @@ fun countGood(nums: IntArray, k: Int): Long {
365365
private fun IntArray.hasSingle() = when {
366366
this.size == 1 -> true
367367
else -> false
368+
}
369+
370+
371+
/**
372+
* 169. Majority Element
373+
*/
374+
375+
fun majorityElement(nums: IntArray): Int {
376+
var majority = 0
377+
var res = 0
378+
val target = nums.size / 2
379+
380+
nums.forEach {
381+
if (majority == 0) res = it
382+
majority += when {
383+
it == res -> 1
384+
else -> -1
385+
}
386+
}
387+
388+
return res
368389
}

0 commit comments

Comments
 (0)