A candidate's strongest rank is the smallest numeric position among valid ballots. Handle a missing candidate before converting an index into a rank.
Worked example
<style>
dl {
display: grid;
gap: 0.6rem;
margin: 0;
}
dl > div {
display: grid;
gap: 0.35rem;
padding: 0.6rem;
border-bottom: 1px solid #bbb;
}
dt {
font-weight: 700;
}
dd {
margin: 0;
overflow-wrap: anywhere;
}
@media (min-width: 32rem) {
dl > div {
grid-template-columns: minmax(5rem, 1fr) 3fr;
}
}
</style>
<h2>Find a candidate's strongest rank</h2>
<details>
<summary>Input records</summary>
<pre>
[
{
"name": "Ada",
"picks": [
"A",
"C",
"B"
]
},
{
"name": "Kim",
"picks": [
"C",
"A",
"B"
]
},
{
"name": "Leo",
"picks": [
"A",
"B",
"C"
]
}
]</pre>
</details>
<dl class="" id="result"></dl>
<script>
const rows = [
{ name: "Ada", picks: ["A", "C", "B"] },
{ name: "Kim", picks: ["C", "A", "B"] },
{ name: "Leo", picks: ["A", "B", "C"] },
];
const result = Math.min(...rows.map((r) => r.picks.indexOf("C") + 1));
const list = document.querySelector("#result");
list.replaceChildren();
for (const [key, value] of Object.entries(
typeof result === "object" && result !== null ? result : { value: result },
)) {
const row = document.createElement("div");
const label = document.createElement("dt");
label.textContent = key;
const detail = document.createElement("dd");
detail.textContent =
typeof value === "string" ? value : JSON.stringify(value);
row.append(label, detail);
list.append(row);
}
if (!list.children.length) list.after("No matching entries.");
</script>Implementation notes
A candidate absent from the data has no observed rank. Giving that candidate rank zero would incorrectly place them above the winner.
Try it yourself
Remove the candidate from one ballot and inspect how an absent index behaves.
Open the UI Components example →Editorial date July 12, 2025 · Published December 6, 2026