Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statistic mode() returns wrong value for some string arrays #41

Open
tmhglnd opened this issue Nov 16, 2024 · 1 comment
Open

Statistic mode() returns wrong value for some string arrays #41

tmhglnd opened this issue Nov 16, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@tmhglnd
Copy link
Owner

tmhglnd commented Nov 16, 2024

When using the mode() on for example the following array: [ 'i', '_', ')', '_', 'B', '_' ] the result is [ '_', ')', '_', 'B', '_' ], but the expected result should be: [ '_' ]

@tmhglnd tmhglnd added the bug Something isn't working label Nov 16, 2024
@tmhglnd
Copy link
Owner Author

tmhglnd commented Nov 16, 2024

Possible solution (still to be implemented and tested and adapted for multi-dimensional arrays)

// find the mode (or modes) in a one-dimensional array
function mode(list){
	// get all the unique occurances and the amount of times they occur
	let occurances = {};
	list.forEach((o) => {
		if (!occurances[o]){
			occurances[o] = 0;
		}
		occurances[o]++;
	});

	// for all the items save the best streak (or streaks)
	let modes = [];
	let streak = 0;
	Object.keys(occurances).forEach((o) => {
		if (occurances[o] > streak){
			streak = occurances[o];
			modes = [o];
		} else if (occurances[o] === streak){
			modes.push(o);
		}
	});

	return modes;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant