Created
January 2, 2024 10:02
-
-
Save vamsitallapudi/dfcb34a76e8b3674b857ab299c68fa29 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean containsDuplicate(int[] nums) { | |
// Using set because it can retreive values in O(1) time. | |
Set<Integer> set = new HashSet<>(); | |
for(int i = 0; i< nums.length; i++) { | |
if (set.contains(nums[i])) { | |
return true; // found duplicate | |
} else { | |
set.add(nums[i]); | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment