N-Repeated Element in Size 2N Array

🏠 ⬅️ ➡️

You are given an integer array nums with the following properties:

  • nums.length == 2 * n.
  • nums contains n + 1 unique elements.
  • Exactly one element of nums is repeated n times.

Return the element that is repeated n times.

Example 1:

Input: nums = [1,2,3,3] Output: 3

Example 2:

Input: nums = [2,1,2,5,3,2] Output: 2

Example 3:

Input: nums = [5,1,5,2,5,3,5,4] Output: 5

Constraints:

  • 2 <= n <= 5000
  • nums.length == 2 * n
  • 0 <= nums[i] <= 104
  • nums contains n + 1 unique elements and one of them is repeated exactly n times.

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

    integer, parameter :: n = 2
    integer, dimension(2*n) :: nums
    integer :: i, j, count
    logical :: found

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

    ! Example 2
    nums = [2, 1, 2, 5, 3, 2]
    write (*, '(A, I0)') 'Example 2: ', find_repeated(nums)

    ! Example 3
    nums = [5, 1, 5, 2, 5, 3, 5, 4]
    write (*, '(A, I0)') 'Example 3: ', find_repeated(nums)

contains

    function find_repeated(nums) result(repeated)
        implicit none
        integer, dimension(:), intent(in) :: nums
        integer :: repeated
        integer :: i, j, count
        logical :: found

        repeated = -1
        do i = 1, size(nums) - 1
            count = 0
            found = .false.
            do j = i + 1, size(nums)
                if (nums(j) == nums(i)) then
                    count = count + 1
                    found = .true.
                end if
            end do
            if (found .and. count == n) then
                repeated = nums(i)
                exit
            end if
        end do
    end function find_repeated

end program main
Compiled
Executed
Correct
module RepeatedNTimes
    implicit none
    private
    public :: repeatedNTimes
contains
    function repeatedNTimes(nums) result(repeatedElement)
        integer, intent(in) :: nums(:)
        integer :: repeatedElement
        integer :: count, i

        count = 0
        repeatedElement = 0

        do i = 1, size(nums)
            if (count == 0) then
                repeatedElement = nums(i)
            end if
            if (nums(i) == repeatedElement) then
                count = count + 1
            else
                count = 0
            end if
        end do

        if (count == 0) then
            write (*,*) "No repeated element found"
        else
            write (*,*) "Repeated element: ", repeatedElement
        end if
    end function repeatedNTimes
end module RepeatedNTimes

program test_repeatedNTimes
    use RepeatedNTimes
    implicit none
    integer, parameter :: n = 4
    integer :: nums(2*n)
    integer :: i

    ! Example 1
    nums = [1, 2, 3, 3]
    call repeatedNTimes(nums)

    ! Example 2
    nums = [2, 1, 2, 5, 3, 2]
    call repeatedNTimes(nums)

    ! Example 3
    nums = [5, 1, 5, 2, 5, 3, 5, 4]
    call repeatedNTimes(nums)

    ! Example 4 (no repeated element)
    nums = [1, 2, 3, 4, 5, 6]
    call repeatedNTimes(nums)
end program test_repeatedNTimes
🌐 Data from online sources
def isLongPressedName(name: str, typed: str) -> bool:
    i, j = 0, 0
    while j < len(typed):
        if i < len(name) and name[i] == typed[j]:
            i += 1
        elif j == 0 or typed[j] != typed[j - 1]:
            return False
        j += 1
    return i == len(name)

We initialize two pointers i and j, pointing to the current characters in the name and typed strings. We iterate through the typed string using the pointer j, and check the following conditions in each iteration:

  1. If the current characters at positions i and j are the same, we increment both pointers i and j.
  2. If the current character in the typed string (j) does not match the character at position j - 1, it means that there is no long press, and we return False since it cannot be a valid typing.

After the loop, we check if the pointer i reached the end of the name string. If it did, it means that all characters in the name string were found at the typed string with some characters possibly being long pressed. If not, it implies that the typed string is not a valid typing of the name string, and we return False.

🌐 Data from online sources
bool isLongPressedName(string name, string typed) {
    int i = 0, j = 0;
    while (j < typed.length()) {
        if (i < name.length() && name[i] == typed[j]) {
            i++;
        } else if (j == 0 || typed[j] != typed[j - 1]) {
            return false;
        }
        j++;
    }
    return i == name.length();
}

We initialize two pointers i and j, pointing to the current characters in the name and typed strings. We iterate through the typed string using the pointer j, and check the following conditions in each iteration:

  1. If the current characters at positions i and j are the same, we increment both pointers i and j.
  2. If the current character in the typed string (j) does not match the character at position j - 1, it means that there is no long press, and we return False since it cannot be a valid typing.

After the loop, we check if the pointer i reached the end of the name string. If it did, it means that all characters in the name string were found at the typed string with some characters possibly being long pressed. If not, it implies that the typed string is not a valid typing of the name string, and we return False.