Find Lucky Integer in an Array

🏠 ⬅️ ➡️

Given an array of integers arr, a lucky integer is an integer that has a frequency in the array equal to its value.

Return the largest lucky integer in the array. If there is no lucky integer return -1.

Example 1:

Input: arr = [2,2,3,4] Output: 2 Explanation: The only lucky number in the array is 2 because frequency[2] == 2.

Example 2:

Input: arr = [1,2,2,3,3,3] Output: 3 Explanation: 1, 2 and 3 are all lucky numbers, return the largest of them.

Example 3:

Input: arr = [2,2,2,3,3] Output: -1 Explanation: There are no lucky numbers in the array.

Constraints:

  • 1 <= arr.length <= 500
  • 1 <= arr[i] <= 500

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

    integer, parameter :: n = 5
    integer, dimension(n) :: arr
    integer :: i, max_freq, max_val

    ! Example 1
    arr = [2, 2, 3, 4]
    write(*, '(A, I0)') 'Example 1: ', max_lucky_int(arr)

    ! Example 2
    arr = [1, 2, 2, 3, 3, 3]
    write(*, '(A, I0)') 'Example 2: ', max_lucky_int(arr)

    ! Example 3
    arr = [2, 2, 2, 3, 3]
    write(*, '(A, I0)') 'Example 3: ', max_lucky_int(arr)

contains

    function max_lucky_int(arr) result(max_val)
        implicit none
        integer, intent(in) :: arr(:)
        integer :: freq(size(arr)), max_freq, max_val

        freq = count(arr == arr)
        max_freq = maxval(freq)
        max_val = -1
        do i = 1, size(arr)
            if (freq(i) == max_freq .and. arr(i) > max_val) then
                max_val = arr(i)
            end if
        end do
    end function max_lucky_int

end program main
Compiled
Executed
Correct
module LuckyInteger
    implicit none
    private
    public :: luckyInteger
contains
    integer function luckyInteger(arr) result(largestLuckyInteger)
        integer, intent(in) :: arr(:)
        integer :: i, frequency(500)

        ! Initialize frequency array
        frequency = 0

        ! Count frequency of each integer in the array
        do i = 1, size(arr)
            frequency(arr(i)) = frequency(arr(i)) + 1
        end do

        ! Find the largest lucky integer
        largestLuckyInteger = -1
        do i = 1, size(arr)
            if (frequency(arr(i)) == arr(i)) then
                if (largestLuckyInteger < arr(i)) then
                    largestLuckyInteger = arr(i)
                end if
            end if
        end do
    end function
end module

program test_luckyInteger
    use LuckyInteger
    implicit none
    integer :: arr(5)

    ! Test case 1
    arr = [2, 2, 3, 4]
    write (*,*) 'Largest lucky integer in ', arr, ' is ', luckyInteger(arr)

    ! Test case 2
    arr = [1, 2, 2, 3, 3, 3]
    write (*,*) 'Largest lucky integer in ', arr, ' is ', luckyInteger(arr)

    ! Test case 3
    arr = [2, 2, 2, 3, 3]
    write (*,*) 'Largest lucky integer in ', arr, ' is ', luckyInteger(arr)
end program
🌐 Data from online sources
def find_lucky(arr):
    freq = {}
    for num in arr:
        freq[num] = freq.get(num, 0) + 1
    max_lucky = -1
    for num, count in freq.items():
        if num == count:
            max_lucky = max(max_lucky, num)
    return max_lucky

The algorithm iterates over the input array, computing the frequency of each number. It then iterates through the frequencies and checks if the number and its frequency match. If they do, it keeps track of the maximum such number. Finally, the algorithm returns the maximum lucky integer. If no lucky integer is found, the function returns -1.

🌐 Data from online sources
int findLucky(vector<int>& arr) {
    unordered_map<int, int> freq;
    for (int num : arr) {
        freq[num]++;
    }
    int maxLucky = -1;
    for (auto& pair : freq) {
        if (pair.first == pair.second) {
            maxLucky = max(maxLucky, pair.first);
        }
    }
    return maxLucky;
}

The algorithm iterates over the input array, computing the frequency of each number. It then iterates through the frequencies and checks if the number and its frequency match. If they do, it keeps track of the maximum such number. Finally, the algorithm returns the maximum lucky integer. If no lucky integer is found, the function returns -1.