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

array_count #103

Open
Appsurdgames opened this issue Dec 7, 2024 · 0 comments
Open

array_count #103

Appsurdgames opened this issue Dec 7, 2024 · 0 comments
Labels
feature ✨ For feature requests and implementations

Comments

@Appsurdgames
Copy link

Appsurdgames commented Dec 7, 2024

Counts the frequency of a certain item occuring in the array.
It may be extended to allow the item to be an array, such that it returns an array of counts.

Here is a possible implementation of the simple version with a single item:

/// @function array_count(array, elt)
/// @param {array}	array	Input array
/// @param {any}	elt		The element to count
/// @return frequency

function array_count(_array, _elt)
{
	if is_array(_array)
	{
		var count = 0;
		for(var i = 0; i<array_length(_array); i++)
		{
			if is_array(_array[i])
			{
				count += array_count(_array[i], _elt);
			}
			else if (_array[i] == _elt)
			{
				count += 1;
			}
		}
		return count;
	}
	else
	{
		return 0;
	}
}
@Alphish Alphish added the feature ✨ For feature requests and implementations label Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ✨ For feature requests and implementations
Projects
None yet
Development

No branches or pull requests

2 participants