Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cc3ee55

Browse files
committed
day 12
1 parent b69330f commit cc3ee55

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
- Day 9: [Is Perfect Square](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-nine/index.ts) :alien:
1313
- Day 10: [Find Town Judge](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-ten/index.ts) :poop:
1414
- Day 11: [Flood Fill](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-eleven/index.ts) :raising_hand:
15+
- Day 12: [Single Element in a Sorted Array](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-twelve/index.ts) :hear_no_evil:

‎src/may/day-twelve/index.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
interface MapObject {
6+
[key: string]: number;
7+
}
8+
9+
export const singleNonDuplicate = function (nums: number[]) {
10+
const mapStr: string[] = nums.map((n) => n.toString());
11+
12+
const map: MapObject = mapStr.reduce((allChar: MapObject, char: string) => {
13+
if (char in allChar) {
14+
allChar[char]++;
15+
} else {
16+
allChar[char] = 1;
17+
}
18+
return allChar;
19+
}, {});
20+
21+
const result: [string, number][] = Object.entries(map).sort((a, b) => a[1] - b[1]);
22+
return Number(result[0][0]);
23+
};
24+
25+
const a = singleNonDuplicate([1, 1, 2, 3, 3, 4, 4, 8, 8]);
26+
console.log('a', a);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /