-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhard.js
75 lines (60 loc) · 1.34 KB
/
hard.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function fizzBuzz(start, end) {
const fizzBuzz = [];
for (let i = start; i < end; i++) {
if (i % 15 === 0) {
fizzBuzz.push("fizzbuzz");
} else if (i % 3 === 0) {
fizzBuzz.push("fizz");
} else if (i % 5 === 0) {
fizzBuzz.push("buzz");
} else {
fizzBuzz.push(i);
}
}
return fizzBuzz;
}
function hasDuplicate(numbers) {
const numberTally = {};
for (const number of numbers) {
if (number in numberTally) {
return true;
}
numberTally[number] = 1;
}
return false;
}
function findPlurality(items) {
if (items.length === 0) {
return [];
}
const itemCounter = {};
for (let item of items) {
if (!(item in itemCounter)) {
itemCounter[item] = 0;
}
++itemCounter[item];
}
const maxCount = Math.max(...Object.values(itemCounter));
return Object.keys(itemCounter).filter(
(item) => itemCounter[item] === maxCount
);
}
function findMajority(items) {
if (items.length === 0) {
return undefined;
}
const itemCounter = {};
for (const item of items) {
if (!(item in itemCounter)) {
itemCounter[item] = 0;
}
itemCounter[item]++;
}
for (const item in itemCounter) {
if (itemCounter[item] > items.length / 2) {
return item;
}
}
return undefined;
}
export { fizzBuzz, hasDuplicate, findPlurality, findMajority };