Australia (and many countries) has a perferential voting system, where voters, number the candidates from 1 - n, where n is the number of candidates, in order of preference. Until a candidate gets a absolute majority of votes, the number 1 votes for the candidate with minimum count of number 1 votes, are redistributed to their next preference. Full detail can be found at http://www.eca.gov.au/systems/single/by_category/preferential.htm, under the The Full Preferential Count
Implement a system that takes in a array of voting slips. where a voting slip is a array, where each index is a candidate ID, and the element at that index is the voters preference number for that candidate.
Eg: for a 3 candidates with, 7 voting slips, input could be:
votes =
2 1 3
1 2 3
3 2 1
1 3 2
1 2 3
2 3 1
3 2 1
You may take this input in your languages preferred 2D array like data structure.
Example function: (matlab)
function winningParty = countVotes (votes)
[nVotes, nParties] = size(votes)
cut = zeros(1,nParties) % no one is cut at the start
while(max(sum(votes==1))<=0.5*nVotes) %while no one has absolute majority
[minVotes, cutParty] = min(sum(votes==1) .+ cut*nVotes*2) %make sure not cut twice, by adding extra votes to cut party
votes(votes(:,cutParty)==1,:)--; % reassign there votes
cut(cutParty) = true;
votes
end
[winnersVotes, winningParty] = max(sum(votes==1))
Output:
octave:135> countVotes(votes)
nVotes = 7
nParties = 3
cut =
0 0 0
minVotes = 1
cutParty = 2
votes =
1 0 2
1 2 3
3 2 1
1 3 2
1 2 3
2 3 1
3 2 1
winnersVotes = 4
winningParty = 1
ans = 1 %This is the only line i care about
EDIT: In cases of tie for lowest portion of primary share, either can be removed. There can be no tie for winning candidate. Winner must have strictly greater than 50% of votes.