-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d563385
commit 6b12ec1
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { mode } from '../src'; | ||
import { generateRandomNums } from '../benchmarks/utils'; | ||
|
||
const n: number = 100_000; | ||
const random_nums: number[] = generateRandomNums(n); | ||
|
||
function mode_simple(x: number[]): number { | ||
const counts = new Map<number, number>(); | ||
for (const xi of x) { | ||
counts.set(xi, (counts.get(xi) || 0) + 1); | ||
} | ||
|
||
let mode = NaN; | ||
let maxCount = 0; | ||
for (const [xi, count] of counts.entries()) { | ||
if (count > maxCount) { | ||
mode = xi; | ||
maxCount = count; | ||
} | ||
} | ||
|
||
return mode; | ||
} | ||
|
||
const mode_res1 = mode(random_nums); | ||
const mode_res3 = mode_simple(random_nums); | ||
|
||
console.log('mode_res1:', mode_res1); | ||
console.log('mode_res3:', mode_res3); |