Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
// Contains Duplicate - https://leetcode.com/problems/contains-duplicate/ class Solution { // Brute force // Time Complexity: O(n^2) // Space Complexity: O(1) // Below solution leads to time limit exceeded on LeetCode // public boolean containsDuplicate(int[] nums) { // for(int i = 0; i < nums.length; i++) { // for(int j = i + 1; j < nums.length; j++) { // if(nums[i] == nums[j]) { // return true; // } // } // } // return false; // } // Using HashSet // Time Complexity: O(n) // Space Complexity: O(n) // public boolean containsDuplicate(int[] nums) { // Set
set = new HashSet<>(); // for(int i = 0; i < nums.length; i++) { // set.add(nums[i]); // } // return set.size() != nums.length; // } // Using HashSet (Optimized) // Time Complexity: O(n) // Space Complexity: O(n) public boolean containsDuplicate(int[] nums) { Set
set = new HashSet<>(); for(int i = 0; i < nums.length; i++) { if(set.contains(nums[i])) { return true; } set.add(nums[i]); } return false; } }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)