Files
2026-05-30 16:02:33 -04:00

1.2 KiB

id, title, difficulty, tags, status, date_solved, leetcode_url, review_needed
id title difficulty tags status date_solved leetcode_url review_needed
169 Majority Element Easy
array
hash-table
sorting
counting
Solved 2026-05-26 https://leetcode.com/problems/majority-element/ false

169. Majority Element

[!info] Problem Link: LeetCode - Majority Element

📝 Problem Description

Given an array nums of size n, return the majority element.

The majority element is the element that appears more than [n / 2] times. You may assume that the majority element always exists in the array.


📥 Example 1

Input: nums = [3,2,3] Output: 3

📥 Example 2

Input: nums = [2,2,1,1,1,2,2] Output: 2


💡 Approaches & Explanations

have cont, if cont is equal to 0 the res become the highest amount if i equal to the res than add one to count anything else cont -1 return res at the end

💻 Code Implementations

Python3

class Solution:
	def majorityElement(self, nums: List[int]) -> int:
		cont = 0
		res = None
		for i in nums:
			if cont == 0:
				res = i
			cont +=1 if res == i else -1
		return res