Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions solution/0100-0199/0169.Majority Element/Solution1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
func findMajorityElement(nums []int) int {
// Create a hashmap to store the frequency of each element
frequency := make(map[int]int)

// Traverse the array and count occurrences of each element
for _, num := range nums {
frequency[num]++
}

// Find the element with the maximum frequency
maxCount := 0
var majorityElement int
for num, count := range frequency {
if count > maxCount {
maxCount = count
majorityElement = num
}
}

// Return the element with the maximum frequency (not checking n/2 times explicitly)
return majorityElement
}
Loading