Skip to content

Commit 485fafe

Browse files
authored
Merge pull request #4 from kotler-dev/exercise/id169
exercise/id169
2 parents 6549a05 + a812d99 commit 485fafe

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[//]: # (Copyright [2023] [Anton Kotler kotler.developer@gmail.com] License MIT)
2+
3+
# Majority Element
4+
5+
![Difficulty](https://img.shields.io/badge/Difficulty-Easy-61904f)
6+
7+
Given an array `nums` of size `n`, return the majority element.
8+
The majority element is the element that appears more than `|n / 2|` times.
9+
You may assume that the majority element always exists in the array.
10+
11+
Example 1:
12+
13+
```
14+
Input: nums = [3,2,3]
15+
Output: 3
16+
```
17+
18+
Example 2:
19+
20+
```
21+
Input: nums = [2,2,1,1,1,2,2]
22+
Output: 2
23+
```
24+
25+
Constraints:
26+
27+
- `n == nums.length`
28+
- `1 <= n <= 5 * 10^4`
29+
- `-10^9 <= nums[i] <= 10^9`
30+
31+
Follow-up: Could you solve the problem in linear time and in `O(1)` space?
32+
33+
| ID | Description | Solution | Test | Difficulty |
34+
|:---:|:-----------------|:----------------------------:|:-----------------------------------------------------------------------------------:|:----------:|
35+
| 169 | Majority Element | [solution](./Solution169.kt) | [test](../../../../../../src/test/kotlin/exercise100/easy/id169/Solution169Test.kt) | Easy |
36+
37+
:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package exercise100.easy.id169
2+
3+
class Solution169 {
4+
fun majorityElement(nums: IntArray): Int {
5+
val hm = HashMap<Int, Int>()
6+
nums.forEach {
7+
val n = hm[it]
8+
if (n == null) hm[it] = 0
9+
if (n != null) hm[it] = n + 1
10+
}
11+
return hm.maxBy { n -> n.value }.key
12+
}
13+
14+
/*
15+
16+
17+
18+
*/
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package exercise100.easy.id169
2+
3+
import io.kotest.assertions.print.print
4+
import io.kotest.core.spec.style.FunSpec
5+
import io.kotest.datatest.WithDataTestName
6+
import io.kotest.datatest.withData
7+
import io.kotest.matchers.shouldBe
8+
9+
data class TestCase(
10+
val prices: IntArray,
11+
val expected: Int,
12+
) : WithDataTestName {
13+
override fun dataTestName(): String = expected.print().value
14+
}
15+
16+
class Solution169Test : FunSpec({
17+
context("shouldBe") {
18+
withData(
19+
nameFn = { "Input: ${it.prices.print().value}, Expected: ${it.expected.print().value}" },
20+
TestCase(prices = intArrayOf(3, 2, 3), expected = 3),
21+
TestCase(prices = intArrayOf(2, 2, 1, 1, 1, 2, 2), expected = 2),
22+
) { (prices, expected) ->
23+
Solution169().majorityElement(prices) shouldBe expected
24+
}
25+
}
26+
})

0 commit comments

Comments
 (0)