-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.countInArray.js
More file actions
32 lines (29 loc) · 826 Bytes
/
12.countInArray.js
File metadata and controls
32 lines (29 loc) · 826 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
const arr = [220, 1, 0, 20, 3, 3, 3, 4, 5, 1, 6, 7, 7, 1, 1, 1, 1]
arr.sort((a, b) => a - b)
function count(array) {
const mapCount = {}
while (array.length > 0) {
const mid = Math.floor(array.length / 2)
let currentMid = mid
mapCount[array[mid]] = 1
let left = 0
let right = 0
while (array[currentMid - 1] === array[mid]) {
mapCount[array[mid]] += 1
currentMid--
left++
}
currentMid = mid
while (array[currentMid + 1] === array[mid]) {
mapCount[array[mid]] += 1
currentMid++
right++
}
let countDelete = left + right + 1
array.splice(mid - left, countDelete)
}
return mapCount
}
console.log(arr);
console.log(count(arr));
console.log(arr);