Skip to content

Commit

Permalink
Add leetcode nim 169 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
BurnySc2 committed Aug 27, 2023
1 parent e443dad commit 6aafbfb
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions problem_sites/leetcode/nim/majority_element.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 169
# https://leetcode.com/problems/majority-element/
# nim js -d:release -o:majority_element_js.js majority_element.nim
# nim js -d:danger -o:majority_element_js.js majority_element.nim

import tables

proc majorityElement(nums: seq[int]): int {.exportc.} =
let cache = newTable[int, int]()
for i in nums:
let count = cache.getOrDefault(i, 0) + 1
if count > nums.len div 2:
return i
cache[i] = count

0 comments on commit 6aafbfb

Please sign in to comment.