Skip to content

Instantly share code, notes, and snippets.

@aomerk
Created March 23, 2022 06:55
Show Gist options
  • Save aomerk/88ec52765b77eddd2868a50046ada96c to your computer and use it in GitHub Desktop.
Save aomerk/88ec52765b77eddd2868a50046ada96c to your computer and use it in GitHub Desktop.
Number of occurences of highest value in solidity
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.0;
function freqOfMaxNum(uint256[] memory arr)
public
pure
returns (uint256, uint256)
{
uint256 max = 0;
uint256 frequenceOfMax = 0;
for (uint256 i = arr.length - 1; i > 0; i--) {
if (arr[i] > max) {
max = arr[i];
frequenceOfMax = 1;
} else if (arr[i] == max) {
frequenceOfMax++;
}
}
return (max, frequenceOfMax);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment