-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathfind-count-of-most-frequent-item-in-an-array.js
128 lines (115 loc) · 3.29 KB
/
find-count-of-most-frequent-item-in-an-array.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
function mostFrequentItemCount(collection) {
// a place to store the number of occurrences
const counts = {};
// a variable to store the current highest count item
let currentHighest;
// iterate over the collection
for (let i = 0; i < collection.length; i += 1) {
const item = collection[i];
// if the item has been seen before
if (counts[item]) {
// increment the stored count
counts[item] += 1;
} else {
// add this item to the number of occurrences variable
// set the count to 1
counts[item] = 1;
}
// if the count for this item is larger than the current highest count item, replace it
if (!currentHighest || currentHighest < counts[item]) {
currentHighest = counts[item];
}
}
// return the highest count
return currentHighest;
}
function mostFrequentItemCount(collection) {
const counts = {};
let currentHighest = 0;
for (let i = 0; i < collection.length; i += 1) {
const item = collection[i];
if (counts[item]) {
counts[item] += 1;
} else {
counts[item] = 1;
}
if (currentHighest < counts[item]) {
currentHighest = counts[item];
}
}
return currentHighest;
}
function mostFrequentItemCount(collection) {
const counts = {};
let currentHighest = 0;
for (let i = 0; i < collection.length; i += 1) {
const item = collection[i];
// check if property exists in an object...
// if (counts[item]) {
// if (counts.hasOwnProperty(item)) {
// if (Object.prototype.hasOwnProperty.call(counts, item)) {
if (Object.hasOwn(counts, item)) {
counts[item] += 1;
} else {
counts[item] = 1;
}
if (currentHighest < counts[item]) {
currentHighest = counts[item];
}
}
return currentHighest;
}
function mostFrequentItemCount(collection) {
const counts = {};
let currentHighest = 0;
for (let i = 0; i < collection.length; i += 1) {
const item = collection[i];
// counts[item] = (counts[item] || 0) + 1;
counts[item] = (Object.hasOwn(counts, item) ? counts[item] : 0) + 1;
if (currentHighest < counts[item]) {
currentHighest = counts[item];
}
}
return currentHighest;
}
function mostFrequentItemCount(collection) {
const counts = new Map();
let currentHighest = 0;
for (let i = 0; i < collection.length; i += 1) {
const item = collection[i];
counts.set(item, (counts.get(item) || 0) + 1);
if (currentHighest < counts.get(item)) {
currentHighest = counts.get(item);
}
}
return currentHighest;
}
function mostFrequentItemCount(collection) {
const counts = new Map();
for (let i = 0; i < collection.length; i += 1) {
const item = collection[i];
counts.set(item, (counts.get(item) || 0) + 1);
}
return Math.max(...counts.values());
}
function mostFrequentItemCount(collection) {
const counts = new Map();
collection.forEach((item) => {
counts.set(item, (counts.get(item) || 0) + 1);
});
return Math.max(...[...counts.values()]);
}
function mostFrequentItemCount(collection) {
return Math.max(...collection.reduce((counts, item) => {
counts.set(item, (counts.get(item) || 0) + 1);
return counts;
}, new Map()).values());
}
console.log(
mostFrequentItemCount([3, -1, -1]),
2
);
console.log(
mostFrequentItemCount([3, -1, -1, -1, 2, 3, -1, 3, -1, 2, 4, 9, 3]),
5
);