Skip to content

Add mode function #42

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

Closed
delphidabbler opened this issue Jan 10, 2025 · 4 comments
Closed

Add mode function #42

delphidabbler opened this issue Jan 10, 2025 · 4 comments
Assignees
Labels
completed Issue completed and committed to develop. To be closed on next release enhancement New feature or request

Comments

@delphidabbler
Copy link
Owner

The following Mode function applies to countable sets. It obeys the rule that if all items in a sequence of values are unique then there is no mode. The following function returns an empty array in that case. If sequence in mono-modal an array of one element containing the mode is returned, while if sequence is multi-modal, an array containing all the modes is returned.

This function illustrates the case for a sequence of Integer values. Overloads for other ordinal types are obvious.

function ModeN(const A: array of Integer): TArray<Integer>;
begin
  if Length(A) = 0 then
    raise EArgumentException.Create('Array is empty');
  var MaxCount: Cardinal := 0;
  var Counts := TDictionary<Integer,Cardinal>.Create;
  try
    for var X in A do
    begin
      var C: Cardinal;
      if not Counts.TryGetValue(X, C) then
        C := 0;
      Inc(C);
      Counts.AddOrSetValue(X, C);
      if MaxCount < C then
        MaxCount := C;
    end;
    // equivalent test?: if MaxCount = 1 then
    if Length(A) = Counts.Count then 
    begin
      // All numbers are unique => no mode
      SetLength(Result, 0);
      Exit;
    end;
    var Results := TList<Integer>.Create;
    try
      for KV in Counts do
      begin
        if KV.Value = MaxCount then
          Results.Add(KV.Key);
      end;
      Result := Results.ToArray;
    finally
      Results.Free;
    end;
  finally
    Counts.Free;
  end;
end;

This issue was extracted from issue #16

@delphidabbler delphidabbler self-assigned this Jan 10, 2025
@delphidabbler delphidabbler added enhancement New feature or request considering Issue is currently under consideration labels Jan 10, 2025
@github-project-automation github-project-automation bot moved this to Considering in Code Snippets Jan 10, 2025
@delphidabbler delphidabbler added accepted Issue will be actioned and removed considering Issue is currently under consideration labels Jan 10, 2025
@delphidabbler delphidabbler moved this from Considering to Accepted in Code Snippets Jan 10, 2025
@delphidabbler
Copy link
Owner Author

delphidabbler commented Jan 15, 2025

It obeys the rule that if all items in a sequence of values are unique then there is no mode. The following function returns an empty array in that case.

There is no consistency in the documentation about how to handle the situation where every element occurs equally often, so the decision has been taken to returns an array of all the unique values of a given array.

For example:

Mode([1,2,3,4]) => [1,2,3,4]
Mode([1,1,2,2,3,3,4,4] => [1,2,3,4]
Mode([2,2,2,2]) => [2]
Mode([0,1,3,3,2,2]) => [2,3]

It has also been decided to require an array of at least two elements.

@delphidabbler
Copy link
Owner Author

There is no consistency in the documentation about how to handle the situation where every element occurs equally often, so the decision has been taken to returns an array of all the unique values of a given array.

This is the same as the approach taken by the Calculator Soup online calculator.

@delphidabbler delphidabbler added the in progress Work has started on this issue label Jan 15, 2025
@delphidabbler delphidabbler moved this from Accepted to In progress in Code Snippets Jan 15, 2025
@delphidabbler
Copy link
Owner Author

Also adding:

  • ModeAlt - As Mode but returns [] when there is no mode.
  • ModeCount - Returns the number of modes in an array.
  • HasMode - Returns True if an array of integers has a mode, False if not.
  • CountOccurences - Helper function that counts the number of occurrences of each unique integer in an array.

@delphidabbler
Copy link
Owner Author

Implemented by merge commit bb32805

@delphidabbler delphidabbler added completed Issue completed and committed to develop. To be closed on next release and removed accepted Issue will be actioned in progress Work has started on this issue labels Jan 16, 2025
@delphidabbler delphidabbler moved this from In progress to Completed in Code Snippets Jan 16, 2025
@delphidabbler delphidabbler added this to the Next Release milestone Jan 16, 2025
@delphidabbler delphidabbler removed this from the Next Release milestone Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed Issue completed and committed to develop. To be closed on next release enhancement New feature or request
Projects
Status: Completed
Development

No branches or pull requests

1 participant