Are there any examples of contested elections that were held to elect a king?
Target Fact Patterns
By this, I mean two main fact patterns.
A king is elected from multiple candidates in a country which previously did not have a monarchy, or had a dynasty that ended because there was no one left in the line of succession, that holds a national election to choose a new king (or queen).
A country has a succession dispute in which two or more people claim the right to be the legitimate monarch of the country, and this is resolved in a national election, in order to prevent or end a civil war.
Of course, if you know of another fact pattern that fits the other definitions here it would be interesting to know about that as well.
It does not matter whether the election that was held actually succeeded in putting someone on the throne. Elections that were attempted but failed to result in the winner becoming a monarch would also be worthy of mentioning in an answer.
King Defined
An elected king, for these purposes, is someone who once elected is not only the monarch of a country (either constitutional or absolute), but who as a result of the election can also legitimately pass on this position to his descendants or extended family members according to a rule of succession that involves the hereditary principle as a matter of right.
In other words, I am not interested in examples of someone being elected as "President for Life" which has happened numerous times, or examples of parent-child pairs of democratic politicians who manage to be elected in an ordinary democratic election system to a term of office for a fixed term of years.
No particular rule of succession following the hereditary principle, however, is required for the monarch to qualify as a king for this purpose.
For example, the Saudi Arabian system in which a successor is chosen from the male descendants of the founder of the dynasty from the senior members of the class of potential heirs, could qualify. Similarly, a system in which succession would pass from mother to daughter, or to siblings before children, in lieu of the English primogeniture down the male line succession system would count. Even a monarchy with a rule of succession that allows the monarch to name his or her successor unilaterally, that usually or often results in the successor being an extended family member would also count as a king for purposes of this question.
Contested Election Defined
Historically Attested Elections
I am interested in only historically attested examples. I am not interested fictional examples (e.g. Queen Amidala of the Star Wars Universe) or mythical examples.
Multiple Genuine Candidates
For purposes of this question, a "contested election" must be an election with multiple candidates.
An election ratifying a single candidate who has already been chosen before the election, for example, in the ratification of the constitution of an existing monarchy which is transitioning to a constitutional monarchy in which the new constitution identifies the existing monarch or dynasty by name, does not count as a "contested election".
For this purposes "write in" candidates with no genuine possibility of winning a significant share of the vote do not suffice to make it a "contested election."
Not Limited To Members Of The Incumbent's Extended Family
A "contested election" does not include an election in which only members of the sitting monarch's extended family can vote (like the succession system of the Saudi Arabian monarchy or the Cambodian monarchy).
A College Of Cardinals Model Is Not A Contested Election
A "contested election" also does not include an election conducted by members of a small body of a few hundred people or less like the modern College of Cardinals that elected the Pope (which has been in place in its current form since the 12th century and in a different incarnation since the 6th century), which is appointed by the incumbent or his recent predecessors on a non-hereditary basis.
For example, a self-perpetuating board of a dozen or two directors of a non-profit who vote for a new CEO of a company that ran the country would not constitute a "contested election" for this purpose.
An Election Could Have A Quite Narrow Franchise
An "election" could, however, include a situation like that surrounding the adoption of the Magna Carta or the assembly of representatives of the various estates that was convened by the monarch shortly before the French Revolution in which the franchise is quite limited (e.g. limited only to aristocrats, or to elders, or to clergy, or to members of a political party, or to members of a particular tribe or caste, or to a council of leaders of tribes or castes) who are not all members of the incumbent's extended family and whose appointment was not primarily within the control of the incumbent as a practical matter.
For example, the kind of election that Hong Kong conducts now for its leaders would constitute a "contested election" even though it gives special preferences, for example, to various industries, rather than being conducted on a one man, one vote basis.
A contested election could also count even if it limits the franchise to just part of the territory ruled by the monarch. For example, if the people of England elected the next king of England, this would count, even if the person elected would serve as king for all of the British Commonwealth. Similarly, an election would count even if only the people of Rome elected the Pope, even though the person elected served the entire world.