Skip to content

Commit b8f908a

Browse files
committed
feat(problems): add leetcode 169
1 parent 85d7764 commit b8f908a

File tree

6 files changed

+158
-6
lines changed

6 files changed

+158
-6
lines changed

‎pnpm-lock.yaml‎

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 169. Majority Element
2+
3+
Given an array `nums` of size `n`, *return the majority element.*
4+
5+
The majority element is the element that appears more than `⌊n / 2⌋` times. You may assume that the majority element always exists in the array.
6+
7+
### Examples
8+
9+
```
10+
Input: nums = [3,2,3]
11+
Output: 3
12+
```
13+
14+
```
15+
Input: nums = [2,2,1,1,1,2,2]
16+
Output: 2
17+
```
18+
19+
### Constraints
20+
21+
- n == nums.length
22+
- 1 <= n <= 5 * 10<sup>4</sup>
23+
- -10<sup>9</sup> <= nums[i] <= 10<sup>9</sup>
24+
25+
**Follow-up:** Could you solve the problem in **linear time and in O(1)** space?
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
importtype{LeetcodeProblem}from"@typings/leetcode-problem";
2+
3+
/**
4+
* Finds the majority element in an array using the Boyer-Moore Voting Algorithm.
5+
*
6+
* The majority element is the element that appears more than half the times in the array.
7+
* This algorithm has a time complexity of O(n) and a space complexity of O(1).
8+
*
9+
* @param nums An array of numbers.
10+
* @returns The majority element in the array.
11+
*/
12+
constboyerMoore=(nums: number[]): number=>{
13+
// Initialize a counter and a candidate variable
14+
letcount=0;
15+
letcandidate: number|null=null;
16+
17+
// Iterate through the array
18+
for(constnumofnums){
19+
if(count===0){
20+
// Set the candidate as the first element if count is 0
21+
candidate=num;
22+
}
23+
24+
// Increment count if the current element is the candidate, decrement otherwise
25+
count+=num===candidate ? 1 : -1;
26+
}
27+
28+
// Return the candidate if it exists (assuming a majority element always exists)
29+
returncandidateasnumber;
30+
};
31+
32+
/**
33+
* Finds the majority element in an array using a hash map.
34+
*
35+
* The majority element is the element that appears more than half the times in the array.
36+
* This solution has a time complexity of O(n) and a space complexity of O(n) due to the hash map usage.
37+
*
38+
* @param nums An array of numbers.
39+
* @returns The majority element in the array, or undefined if no majority exists.
40+
*/
41+
consthashMap=(nums: number[]): number|undefined=>{
42+
// Create a new map to store number counts
43+
constnumMap=newMap<number,number>();
44+
45+
// Iterate through the array to count occurrences of each number
46+
for(constnumofnums){
47+
// If the number already exists in the map, increment its count, otherwise set count to 1
48+
numMap.has(num) ? numMap.set(num,(numMap.get(num)asnumber)+1) : numMap.set(num,1);
49+
50+
// If the count of the current number exceeds half the length of the array, it's the majority element
51+
if((numMap.get(num)asnumber)>nums.length/2)returnnum;
52+
}
53+
54+
// No majority element found, return undefined
55+
returnundefined;
56+
};
57+
58+
exportconstMAJORITY_ELEMENT: LeetcodeProblem<number|undefined>={
59+
name: "169. Majority Element",
60+
code: "1",
61+
tags: ["Array"],
62+
solutions: [
63+
{name: "HashMap",implementation: hashMap},
64+
{name: "Boyer Moore",implementation: boyerMoore},
65+
],
66+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import{bench,describe}from"vitest";
2+
3+
import{MAJORITY_ELEMENT}from"..";
4+
import{TEST_DATA}from"./test-data";
5+
6+
const{ name, solutions }=MAJORITY_ELEMENT;
7+
8+
describe(name,()=>{
9+
for(const{ name, implementation }ofsolutions){
10+
bench(name,()=>{
11+
for(const{ input }ofTEST_DATA){
12+
implementation(input);
13+
}
14+
});
15+
}
16+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import{describe,expect,it}from"vitest";
2+
3+
import{MAJORITY_ELEMENT}from"..";
4+
import{TEST_DATA}from"./test-data";
5+
6+
describe(MAJORITY_ELEMENT.name,()=>{
7+
// Loop through different implementations provided in solutions
8+
describe.each(MAJORITY_ELEMENT.solutions)("$name",({ implementation })=>{
9+
// Test for each test case
10+
it.each(TEST_DATA)(
11+
"should find the majority element in the array ($expected)",
12+
({ expected, input })=>{
13+
// Validate test data
14+
expect(input).toBeDefined();
15+
expect(expected).toBeDefined();
16+
17+
// Run the test
18+
constresult=implementation(input);
19+
20+
// Validate the output - ensure the result matches the expected indices
21+
expect(result).toBeDefined();
22+
expect(result).toEqual(expected);
23+
},
24+
);
25+
});
26+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
importtype{InputTestData}from"@typings/input-data";
2+
3+
exportconstTEST_DATA: InputTestData<number[],number>[]=[
4+
{input: [3,2,3],expected: 3},
5+
{input: [2,2,1,1,1,2,2],expected: 2},
6+
{input: [1,2,2,3,2],expected: 2},
7+
{input: [5,5,2,2,5,5,5],expected: 5},
8+
{input: [1,1,2,2,3,3,3,3,3],expected: 3},
9+
{input: [6,5,5,5,5,5,5,5,5,5],expected: 5},
10+
{input: [9,9,9,9,9,9,9,9,9,8],expected: 9},
11+
{input: [-1,-2,-2,-2,-2,-2,-2,-2,-2,-2],expected: -2},
12+
{
13+
input: [
14+
1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,999999999,1,2,3,
15+
],
16+
expected: 1000000000,
17+
},
18+
{
19+
input: [
20+
-1000000000,-1000000000,-1000000000,-1000000000,-1000000000,-1000000000,-999999999,1,
21+
2,3,
22+
],
23+
expected: -1000000000,
24+
},
25+
];

0 commit comments

Comments
(0)