-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargestUniqueNumber.js
More file actions
57 lines (46 loc) · 862 Bytes
/
largestUniqueNumber.js
File metadata and controls
57 lines (46 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 1133. Largest Unique Number
// Easy
// 68
// 6
// Add to List
// Share
// Given an array of integers A, return the largest integer that only occurs once.
// If no integer occurs once, return -1.
var largestUniqueNumber = function (A) {
const frequencyMap = {};
for (num of A) {
frequencyMap[num] ? frequencyMap[num]++ : (frequencyMap[num] = 1);
}
let max = -1;
for (let [key, value] of Object.entries(frequencyMap)) {
if (value == 1 && Number.parseInt(key, 10) > Number.parseInt(max, 10)) {
max = key;
}
}
return max;
};
console.log(largestUniqueNumber([5, 7, 3, 9, 4, 9, 8, 3, 1])); // 8
console.log(
largestUniqueNumber([
397,
513,
784,
485,
253,
360,
924,
37,
97,
624,
743,
203,
406,
77,
23,
123,
748,
309,
230,
669,
])
); //924