Given an integer array nums
, return true
if any value appears at least twice in the array, and return false
if every element is distinct.
Example 1:
Input: nums = [1,2,3,1] Output: true
Example 2:
Input: nums = [1,2,3,4] Output: false
Example 3:
Input: nums = [1,1,1,3,3,4,3,2,4,2] Output: true
Constraints:
1 <= nums.length <= 105
-109 <= nums[i] <= 109
program main
implicit none
integer, parameter :: n = 10
integer, dimension(n) :: nums
logical :: result
! Example 1
nums = [1, 2, 3, 1]
result = repeated_element(nums)
write (*, '(L1)') result
! Example 2
nums = [1, 2, 3, 4]
result = repeated_element(nums)
write (*, '(L1)') result
! Example 3
nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2]
result = repeated_element(nums)
write (*, '(L1)') result
contains
function repeated_element(nums) result(result_)
implicit none
integer, intent(in) :: nums(:)
logical :: result_
integer :: i, j
do i = 1, size(nums) - 1
do j = i + 1, size(nums)
if (nums(i) == nums(j)) then
result_ = .true.
exit
end if
end do
if (result_) exit
end do
end function repeated_element
end program main
temp.f95:9:4: 9 | nums = [1, 2, 3, 1] | 1 Error: Different shape for array assignment at (1) on dimension 1 (10 and 4) temp.f95:14:4: 14 | nums = [1, 2, 3, 4] | 1 Error: Different shape for array assignment at (1) on dimension 1 (10 and 4)
```
program duplicate_elements
implicit none
integer, parameter :: int64 = selected_int_kind(13)
integer, dimension(:), allocatable :: nums
logical :: has_duplicates
! read input
read (*, *) nums
! check for duplicates
has_duplicates = .false.
do i = 1, size(nums) - 1
do j = i + 1, size(nums)
if (nums(i) == nums(j)) then
has_duplicates = .true.
exit
end if
end do
if (has_duplicates) exit
end do
! output result
if (has_duplicates) then
write (*, *) "true"
else
write (*, *) "false"
end if
contains
pure function contains_duplicate(nums) result(has_duplicates)
implicit none
integer(int64), dimension(:), intent(in) :: nums
logical :: has_duplicates
integer :: i, j
has_duplicates = .false.
do i = 1, size(nums) - 1
do j = i + 1, size(nums)
if (nums(i) == nums(j)) then
has_duplicates = .true.
exit
end if
end do
if (has_duplicates) exit
end do
end function contains_duplicate
end program duplicate_elements
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:50:2: 50 | ``` | 1 Error: Invalid character in name at (1) temp.f95:13:8: 13 | do i = 1, size(nums) - 1 | 1 Error: Symbol āiā at (1) has no IMPLICIT type temp.f95:14:12: 14 | do j = i + 1, size(nums) | 1 Error: Symbol ājā at (1) has no IMPLICIT type
def containsDuplicate(nums):
return len(nums) != len(set(nums))
The algorithm starts by creating an empty set. Then it iterates through the input array. For each number in the array, it checks if the number is already in the set. If the number is in the set, it means there's a duplicate, so it returns true. If the number is not in the set, it adds the number to the set. If the function has not returned true after iterating through the entire array, it means that there are no duplicates, so it returns false.
In Python, we use the fact that sets don't allow duplicates, so we can directly compare the length of the original list with the length of the set created from the list. If they are not equal, it means there are duplicates in the list, and the function returns true. Otherwise, it returns false.
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> s;
for (int num : nums) {
if (s.find(num) != s.end()) {
return true;
}
s.insert(num);
}
return false;
}
The algorithm starts by creating an empty set. Then it iterates through the input array. For each number in the array, it checks if the number is already in the set. If the number is in the set, it means there's a duplicate, so it returns true. If the number is not in the set, it adds the number to the set. If the function has not returned true after iterating through the entire array, it means that there are no duplicates, so it returns false.
In Python, we use the fact that sets don't allow duplicates, so we can directly compare the length of the original list with the length of the set created from the list. If they are not equal, it means there are duplicates in the list, and the function returns true. Otherwise, it returns false.