mediumHashingPattern: Mixed
Detect Duplicate Values Solution
Problem Statement
You are given an array of integers `values`, determine if there are any duplicate values in the collection. Implement a function to identify and report the presence of duplicate entries.
Examples
Example 1:
Input:[1, 2, 3, 4, 5]
Output:false
Explanation: No duplicate values are present in the array.
Example 2:
Input:[1, 2, 2, 4, 5]
Output:true
Explanation: The value 2 is duplicated in the array.
Constraints
- 1 <= values.length <= 10^5
- -10^9 <= values[i] <= 10^9
- values contains only integers
Time: O(n) Space: O(n)
An optimized approach to detecting duplicate values involves utilizing a hashing data structure, such as a set, to store unique elements from the array. By iterating through the array and checking for the presence of each element in the set, we can identify duplicates efficiently with a time complexity of O(n). The space complexity is also O(n) due to the storage of unique elements.
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
import sys
values = list(map(int, input().split()))
unique_values = set()
duplicates = set()
for num in values:
if num in unique_values:
duplicates.add(num)
else:
unique_values.add(num)
print("Duplicate values found" if len(duplicates) > 0 else "No duplicate values found")