Question Details
Problem Requirements You are given a list of whole numbers called nums. Your goal is to check if any number appears more than once in this list. * If any number shows up at least twice,
return `tr
Full Details
Problem Requirements You are given a list of whole numbers called nums. Your goal is to check if any number appears more than once in this list. * If any number shows up at least twice,
return true. * If every number in the list is unique,
return false.
Sample Cases
Case 1: *
Input: nums = [1, 2, 3, 3] *
Result: true
Case 2: *
Input: nums = [1, 2, 3, 4] *
Result: false ***
Solution Strategy To solve this efficiently, we can use a
HashSet. A HashSet is a tool that allows us to quickly check if we have seen a number before. Here is the step-by-step logic: 1. Create an empty
HashSet. 2. Loop through each number in the nums array. 3. For every number, check if it is already inside the
HashSet. *
If yes: This means we have seen it before. We found a duplicate, so we return true. *
If no: We add the number to the
HashSet and check the next one. 4. If we check all numbers and find no duplicates,
return false. This approach is fast because checking a
HashSet takes
O(1) time on average. # Code Implementation
java class Solution { public boolean containsDuplicate(int[] nums) { // Create a HashSet to store numbers we have seen HashSet<Integer> seen = new HashSet<>(); // Loop through the array for (int num : nums) { // If the number is already in the set, we found a duplicate if (seen.contains(num)) {
**return** true; } // Otherwise, add the number to the set seen.add(num); } // If no duplicates are found after checking all numbers return false; } }
Complexity Analysis *
Time Complexity:
O(n) * We go through the list of numbers exactly once. *
Space Complexity:
O(n) * In the worst case (no duplicates), we store every number in the
HashSet.
About This Question
This is a reported interview question from a netflix interview for a swe role reported in 2025.
It covers the following topics: Array, Hash Table, Arrays .