Majority Vote Classifier (KNN Vote Step)

Basics
BasicsKNNEnsemblesGoogleMetaAmazon

Problem

Implement majority_vote(neighbor_labels) that returns the most frequent integer label from a non-empty list of neighbor votes. If there is a tie, return the smallest label.

Why It Matters: Majority voting is the final decision step of K-Nearest Neighbors and Random Forest / bagging ensembles.

Sample Inputs & Expected Outputs

Example 1 — Clear majority

>>> majority_vote([1, 0, 1, 2, 1])
1

Class 1 appears 3 times.

Example 2 — Two-way tie

>>> majority_vote([2, 1, 2, 1])
1

Classes 1 and 2 both appear twice; smallest label (1) wins.

Example 3 — Three-way tie

>>> majority_vote([5, 3, 4])
3

All appear once; smallest label (3) wins.

Constraints & Edge Cases

  • Return the winning integer label; break ties by smallest label value.

Constraints & Evaluation Criteria

  • Return the winning integer label; break ties by smallest label value.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[1, 0, 1, 2, 1]
Expected Output / Criteria
1