Skip to content

Commit

Permalink
Create topkfrequent.go
Browse files Browse the repository at this point in the history
  • Loading branch information
rerichardjr authored Sep 26, 2023
1 parent 25fe5a4 commit 1f28fa1
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions topkfrequent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
func topKFrequent(nums []int, k int) []int {
//need to write something better, seems like this could be improved
result := make(map[int]int)
count := make(map[int][]int)
topK := []int{}
for j := range nums {
result[nums[j]]++
}
for i, j := range result {
count[j] = append(count[result[i]], i)
}
for i := len(nums); i >= 0; i-- {
if _, ok := count[i]; ok {
if len(topK) != k {
topK = append(topK, count[i]...)
}
}
}
return topK
}

0 comments on commit 1f28fa1

Please sign in to comment.