Largest Unique Number

🏠 ⬅️ ➡️

Given an integer array nums, return the largest integer that only occurs once. If no integer occurs once, return -1.

Example 1:

Input: nums = [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 is the answer.

Example 2:

Input: nums = [9,9,8,8] Output: -1 Explanation: There is no number that occurs only once.

Constraints:

  • 1 <= nums.length <= 2000
  • 0 <= nums[i] <= 1000

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 9
    integer, dimension(n) :: nums
    integer :: i, j, max_occurence, unique_num

    ! Example 1
    nums = [5, 7, 3, 9, 4, 9, 8, 3, 1]
    write (*, '(A, I0)') 'Example 1: ', largest_unique_integer(nums)

    ! Example 2
    nums = [9, 9, 8, 8]
    write (*, '(A, I0)') 'Example 2: ', largest_unique_integer(nums)

contains

    function largest_unique_integer(nums) result(unique_num)
        implicit none
        integer, intent(in) :: nums(:)
        integer :: i, j, max_occurence, unique_num

        max_occurence = 0
        unique_num = -1

        do i = 1, size(nums)
            do j = 1, size(nums)
                if (nums(i) == nums(j)) then
                    max_occurence = max(max_occurence, 2)
                end if
            end do
            if (max_occurence == 1) then
                unique_num = nums(i)
                exit
            else
                max_occurence = 0
            end if
        end do
    end function largest_unique_integer

end program main
Compiled
Executed
Correct
module LargestInteger
    implicit none
    private
    public :: largest_integer

contains

    function largest_integer(nums) result(largest)
        integer, intent(in) :: nums(:)
        integer :: largest
        integer :: i, count

        largest = -1
        do i = 1, size(nums)
            count = 0
            do j = 1, size(nums)
                if (nums(i) == nums(j)) then
                    count = count + 1
                end if
            end do
            if (count == 1) then
                largest = nums(i)
                exit
            end if
        end do
    end function largest_integer
end module LargestInteger

program test_largest_integer
    use LargestInteger
    implicit none
    integer, parameter :: nums1(8) = [5, 7, 3, 9, 4, 9, 8, 3]
    integer, parameter :: nums2(4) = [9, 9, 8, 8]
    integer :: largest

    largest = largest_integer(nums1)
    write (*,*) 'Example 1:', largest

    largest = largest_integer(nums2)
    write (*,*) 'Example 2:', largest
end program test_largest_integer
🌐 Data from online sources
def last_substring(s: str) -> str:
    i, j, k, n = 0, 1, 0, len(s)
    while j + k < n:
        if s[i + k] == s[j + k]:
            k += 1
        elif s[i + k] < s[j + k]:
            i = j
            j += 1
            k = 0
        else:
            j += 1
            k = 0
    return s[i:]

Iterate through the string using three pointers i, j, and k. At each step, compare s[i+k] and s[j+k]. If s[i+k] < s[j+k], set i=j and increment j. If s[i+k] == s[j+k], increment k to continue comparing the next characters. If s[i+k] > s[j+k], just increment j. When j+k reaches the end of the string, the last substring in lexicographical order will start at index i.

🌐 Data from online sources
std::string lastSubstring(std::string s) {
    int i = 0, j = 1, k = 0, n = s.length();
    while (j + k < n) {
        if (s[i + k] == s[j + k]) {
            k++;
        } else if (s[i + k] < s[j + k]) {
            i = j;
            j++;
            k = 0;
        } else {
            j++;
            k = 0;
        }
    }
    return s.substr(i);
}

Iterate through the string using three pointers i, j, and k. At each step, compare s[i+k] and s[j+k]. If s[i+k] < s[j+k], set i=j and increment j. If s[i+k] == s[j+k], increment k to continue comparing the next characters. If s[i+k] > s[j+k], just increment j. When j+k reaches the end of the string, the last substring in lexicographical order will start at index i.