Find median of an array of uints
See original GitHub issue🧐 Motivation
We are building a reliable price oracle for a decentralized exchange (the DutchX). We would like to take the last n prices, and output the median price. So we need an algorithm that is able to find the median of an array of, say, uints.
📝 Details
A function that takes an array and returns its median. Signature:
function findMedian(uint[] memory array) public Pure Returns (uint)
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Program for Mean and median of an unsorted array
To find median: · First, simply sort the array · Then, check if the number of elements present in the array is even...
Read more >How to calculate the median of an array? - java - Stack Overflow
Try sorting the array first. Then after it's sorted, if the array has an even amount of elements the mean ...
Read more >C program to calculate median of an array - Includehelp.com
Basically a median is the value present at the centre of a sorted array list. To calculate the median first we need to...
Read more >How to get the median of an array of numbers in JavaScript
You can easily find the median if the number of elements is odd, but if the count is even, then we need to...
Read more >Java Program To Calculate Median Array | 4 Methods
Calculate Median Array – Standard Method · if(n%2==1) · If the number of elements is odd then, the center-most element is the median....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Digging up this old topic just to propose an implementation:
Ended up implementing this by myself 😃 To avoid moving around large arrays in evm, I used a linked list. The code is here https://github.com/gnosis/dx-price-oracle/blob/fcf2dd0fac65754ed1f99f899cb95799892f5d5a/contracts/DutchXPriceOracle.sol#L80
To make it reusable, all that has to be done is:
Just my two cents ✌️