Post

Leetcode - Largest Unique Number(Premium)

Leetcode - Largest Unique Number(Premium)

Hits

  • Given an array of integers, identify the highest value that appears only once in the array. If no such number exists, return -1.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
type Solution struct{}

func (sol Solution) largestUniqueNumber(A []int) int {
	freqMap := make(map[int]int)
	largestVal := -1
	for _, val := range A{
		freqMap[val]++
	}

	for _, val := range A{
		if freqMap[val]==1 && val>largestVal{
			largestVal= val
		}
	}
	return largestVal
}

This post is licensed under CC BY 4.0 by the author.