Contains Duplicate LeetCode Solution in Java
LeetCode is a popular platform for practicing coding problems and honing your programming skills. One of the must-do questions on LeetCode is the “Contains Duplicate” problem. In this blog post, we will explore this problem, provide a detailed solution in Java, and illustrate it with examples,Contains Duplicate LeetCode Solution in Java.
Problem Statement
The “Contains Duplicate” problem is a common coding challenge that asks you to determine if a given array contains any duplicates. The problem statement can be summarized as follows:
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
This problem can be approached using various techniques, but we’ll focus on solving it using Java.
Solution Approach
To solve this problem, we can use a hash set to keep track of the unique elements in the array as we iterate through it. Here’s a step-by-step approach to solving the problem:
- Initialize an empty hash set to store unique elements.
- Iterate through the array.
- For each element in the array, check if it already exists in the hash set.
- If it does, return
true
because we’ve found a duplicate. - If it doesn’t, add the element to the hash set to mark it as seen.
- After completing the loop and not finding any duplicates, return
false
because all elements are distinct.
Let’s implement this solution in Java with code examples.
Java Code Implementation
import java.util.HashSet;
import java.util.Set;
public class ContainsDuplicate {
public static boolean containsDuplicate(int[] nums) {
// Create a hash set to store unique elements.
Set<Integer> seen = new HashSet<>();
// Iterate through the array.
for (int num : nums) {
// Check if the element is already in the set.
if (seen.contains(num)) {
return true; // Found a duplicate.
}
// Add the element to the set.
seen.add(num);
}
// No duplicates found.
return false;
}
public static void main(String[] args) {
// Example 1: Array contains duplicates
int[] nums1 = {1, 2, 3, 1};
boolean result1 = containsDuplicate(nums1);
System.out.println("Example 1: " + result1); // Output: true
// Example 2: Array does not contain duplicates
int[] nums2 = {1, 2, 3, 4};
boolean result2 = containsDuplicate(nums2);
System.out.println("Example 2: " + result2); // Output: false
// Example 3: Empty array
int[] nums3 = {};
boolean result3 = containsDuplicate(nums3);
System.out.println("Example 3: " + result3); // Output: false
}
}
In this Java code, we’ve defined a containsDuplicate
method that takes an array of integers as input and returns true
if the array contains any duplicates or false
if all elements are distinct. We use a HashSet
called seen
to keep track of the unique elements as we iterate through the array.
Examples
Let’s go through some examples to test our solution:
Example 1: Array contains duplicates
int[] nums1 = {1, 2, 3, 1};
boolean result1 = containsDuplicate(nums1);
System.out.println("Example 1: " + result1); // Output: true
In this example, the array nums1
contains the duplicate element 1
, so the containsDuplicate
method should return true
.
Example 2: Array does not contain duplicates
int[] nums2 = {1, 2, 3, 4};
boolean result2 = containsDuplicate(nums2);
System.out.println("Example 2: " + result2); // Output: false
In this example, all elements in the array nums2
are distinct, so the containsDuplicate
method should return false
.
Example 3: Empty array
int[] nums3 = {};
boolean result3 = containsDuplicate(nums3);
System.out.println("Example 3: " + result3); // Output: false
In this example, the array nums3
is empty, and since there are no elements, there are no duplicates. Thus, the containsDuplicate
method should return false
.
Complexity Analysis
Let’s analyze the time and space complexity of our solution:
- Time Complexity: The time complexity of this solution is O(n), where n is the number of elements in the input array. This is because we iterate through the array once, and each set operation (add or contains) in the hash set is O(1) on average.
- Space Complexity: The space complexity is O(n) as well. In the worst case, when all elements in the array are unique, the hash set will store all n elements.
Conclusion
The “Contains Duplicate” problem is a common coding challenge that can be efficiently solved using a hash set in Java. By keeping track of unique elements and checking for duplicates as we iterate through the array, we can determine whether an array contains any duplicates or if all elements are distinct.
This LeetCode problem not only helps you practice array manipulation and set data structures but also reinforces your understanding of time and space complexity analysis. It’s an essential problem to tackle as you progress in your coding journey.
We hope this blog post has provided you with a clear and simple solution to the “Contains Duplicate” problem on LeetCode using Java. Happy coding!
Thank You!
Read More –
Invert binary Tree | Invert Binary Tree or Mirror Tree LeetCode Solution in Java | Code with Kamlesh – https://kamleshsingad.com/invert-binary-tree-or-mirror-tree-leetcode-solution-in-java/
Merge Two Binary Trees – Leetcode-617 | Merge Two Binary Trees LeetCode Solution in Java – https://kamleshsingad.com/how-to-merge-two-binary-trees-solution-in-java/
206. Reverse Linked List | Reverse Linked List LeetCode Solution in Java | Code with Kamlesh – https://kamleshsingad.com/what-is-reverse-linked-list-leetcode-solution-in-java/