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.
Examples:
Input: nums[] = [4, 5, 6, 4]
Output: true
Explanation: 4 appears twice.Input: nums[] = [1, 2, 3, 4]
Output: false
Explanation: all are distinct.
Check if the given input contains duplicates using Set:
- Insert all the values in the set data structure.
- Check if the size of the set is equal to the array then it means all the elements in the array are unique as the set stores unique elements so return false.
- else array contains the duplicate elements so return true.
Below is the implementation of the above approach:
C++
// C++ code for the above approach: #include <bits/stdc++.h> using namespace std;
bool check(vector< int >& nums)
{ unordered_set< int > s;
// Inserting the elements
for ( int i = 0; i < nums.size(); i++) {
s.insert(nums[i]);
}
// Checking the condition if same size
// return true else false
if (s.size() < nums.size())
return true ;
return false ;
} // Driver code int main()
{ vector< int > nums = { 1, 2, 3, 4 };
// Function Call
cout << (check(nums) ? "Yes" : "No");
return 0;
} |
Java
import java.util.HashSet;
import java.util.Set;
public class Main {
public static boolean checkDuplicates( int [] nums) {
Set<Integer> set = new HashSet<>();
// Inserting the elements into the set
for ( int num : nums) {
if (!set.add(num)) {
// If the element is already present in the set, it's a duplicate
return true ;
}
}
// No duplicates found
return false ;
}
public static void main(String[] args) {
int [] nums = { 1 , 2 , 3 , 4 };
// Function Call
System.out.println(checkDuplicates(nums) ? "Yes" : "No" );
}
} |
Python3
# Python program for the above approach def check(nums):
# Create a set to store unique elements from the list
unique_set = set (nums)
# Check if the length of the set is less than the length of the original list
# If true, it means there are duplicate elements
return len (unique_set) < len (nums)
# Driver code if __name__ = = "__main__" :
nums = [ 1 , 2 , 3 , 4 ]
# Function Call
result = "Yes" if check(nums) else "No"
# Output the result
print (result)
# This code is contributed by Susobhan Akhuli |
C#
using System;
using System.Collections.Generic;
public class GFG
{ public static bool CheckDuplicates( int [] nums)
{
HashSet< int > set = new HashSet< int >();
// Inserting the elements into the set
foreach ( int num in nums)
{
if (! set .Add(num))
{
// If the element is already present in the set, it's a duplicate
return true ;
}
}
// No duplicates found
return false ;
}
public static void Main( string [] args)
{
int [] nums = { 1, 2, 3, 4 };
// Function Call
Console.WriteLine(CheckDuplicates(nums) ? "Yes" : "No" );
}
} |
Javascript
function GFG(nums) {
// Using a Set to store unique elements
let set = new Set(nums);
return set.size < nums.length;
} // Driver code function main() {
let nums = [1, 2, 3, 4];
// Function call
console.log(GFG(nums) ? "Yes" : "No" );
} main(); |
Output
No
Time Complexity: O(n)
Auxiliary space: O(n), n is the size of the array.