Leetcode - Word Search
Leetcode - Two Sum Less Than K

Leetcode - Largest Unique Number

violet posted @ Mar 28, 2020 01:23:18 AM in 算法 with tags Algorithm Golang count sort , 256 阅读

https://leetcode.com/problems/largest-unique-number/

Given an array of integers A, return the largest integer that only occurs once.

If no integer occurs once, return -1.

 

Example 1:

Input: [5,7,3,9,4,9,8,3,1]
Output: 8
Explanation: 
The maximum integer in the array is 9 but it is repeated. The number 8 occurs only once, so it's the answer.

Note:

  1. 1 <= A.length <= 2000
  2. 0 <= A[i] <= 1000

 

Typically count sort problem.

func largestUniqueNumber(A []int) int {
    count := make([]int, 1001)
    for _, n := range A {
        count[n]++
    }
    result := -1
    for i := len(count)-1; i >= 0; i-- {
        if count[i] == 1 {
            result = i
            break
        }
    }
    return result
}
Jimmy 说:
May 01, 2024 04:19:39 AM

Solve the Leetcode problem "Largest Unique Number" using Go. Implement a count sort algorithm to find the largest integer occurring only once in the array. Lake Norman Real Estate


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter