Check if the given input contains Duplicates
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:
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;
bool checkDuplicates(vector<int>& nums)
{
unordered_set<int> set;
// Inserting the elements into the set
for (int num : nums) {
if (!set.insert(num).second) {
// If the element is already present in the set,
// it's a duplicate
return true;
}
}
// No duplicates found
return false;
}
int main()
{
vector<int> nums = { 1, 2, 3, 4 };
// Function Call
cout << (checkDuplicates(nums) ? "Yes" : "No") << endl;
return 0;
}
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");
}
}
# 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
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");
}
}
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.
Check if the given input contains duplicates using Sorting
- Sort the input array nums[].
- Iterate through the sorted array and check if adjacent elements are equal.
- If any adjacent elements are equal, return true indicating that duplicates are present.
- If the entire array is traversed without the finding any duplicates return false.
Below is the implementation of the above approach:
#include <algorithm>
#include <iostream>
#include <vector>
bool GFG(std::vector<int>& nums)
{
// Sort the input array
std::sort(nums.begin(), nums.end());
// Iterate through the sorted array
for (int i = 1; i < nums.size(); ++i) {
// Check if adjacent elements are equal
if (nums[i] == nums[i - 1]) {
return true;
}
}
return false;
}
// Driver code
int main()
{
std::vector<int> nums1 = { 4, 5, 6, 4 };
std::vector<int> nums2 = { 1, 2, 3, 4 };
std::cout << "Duplicates in nums1? "
<< (GFG(nums1) ? "Yes" : "No") << std::endl;
std::cout << "Duplicates in nums2? "
<< (GFG(nums2) ? "Yes" : "No") << std::endl;
return 0;
}
import java.util.Arrays;
public class Main {
static boolean containsDuplicate(int[] nums)
{
// Sort the input array
Arrays.sort(nums);
// Iterate through the sorted array
for (int i = 1; i < nums.length; ++i) {
// Check if adjacent elements are equal
if (nums[i] == nums[i - 1]) {
return true;
}
}
return false;
}
// Driver code
public static void main(String[] args)
{
int[] nums1 = { 4, 5, 6, 4 };
int[] nums2 = { 1, 2, 3, 4 };
System.out.println(
"Duplicates in nums1? "
+ (containsDuplicate(nums1) ? "Yes" : "No"));
System.out.println(
"Duplicates in nums2? "
+ (containsDuplicate(nums2) ? "Yes" : "No"));
}
}
def contains_duplicate(nums):
# Sort the input array
nums.sort()
# Iterate through the sorted array
for i in range(1, len(nums)):
# Check if adjacent elements are equal
if nums[i] == nums[i - 1]:
return True
return False
# Driver code
def main():
nums1 = [4, 5, 6, 4]
nums2 = [1, 2, 3, 4]
print("Duplicates in nums1?", "Yes" if contains_duplicate(nums1) else "No")
print("Duplicates in nums2?", "Yes" if contains_duplicate(nums2) else "No")
main()
function GFG(nums) {
// Sort the input array
nums.sort((a, b) => a - b);
// Iterate through the sorted array
for (let i = 1; i < nums.length; ++i) {
// Check if adjacent elements are equal
if (nums[i] === nums[i - 1]) {
return true;
}
}
return false;
}
// Driver code
let nums1 = [4, 5, 6, 4];
let nums2 = [1, 2, 3, 4];
console.log("Duplicates in nums1? " + (GFG(nums1) ? "Yes" : "No"));
console.log("Duplicates in nums2? " + (GFG(nums2) ? "Yes" : "No"));
Output
Duplicates in nums1? Yes Duplicates in nums2? No
Time Complexity: O(n log n)
Auxiliary space: O(1)