Blind 75
Contains Duplicate
- Problem
- LC 217
- Category
- Array
- File
- blind75_LC217ContainsDuplicate.java
- Path
- pkg5leetcode/blind75/blind75_LC217ContainsDuplicate.java
- Package
- pkg5leetcode.blind75
- Command
- java pkg5leetcode/blind75/blind75_LC217ContainsDuplicate.java
- Approach
- HashSet detects repeated values.
- Complexity
- Time O(n), Space O(n)
There is no in-browser runner. This is the file from the curriculum, unchanged.
1package pkg5leetcode.blind75;2 3/*4 * Contains Duplicate | LC 2175 * APPROACH: HashSet detects repeated values.6 * COMPLEXITY: Time O(n), Space O(n)7 */8import java.util.*;9 10public class blind75_LC217ContainsDuplicate {11 static boolean containsDuplicate(int[] nums) {12 Set<Integer> seen = new HashSet<>();13 for (int x : nums) if (!seen.add(x)) return true;14 return false;15 }16 17 public static void main(String[] args) {18 check(containsDuplicate(new int[]{1, 2, 3, 1}), "case1");19 check(!containsDuplicate(new int[]{1, 2, 3, 4}), "case2");20 System.out.println("all tests passed");21 }22 23 static void check(boolean cond, String name) {24 if (!cond) throw new AssertionError("FAILED: " + name);25 System.out.println(" PASS " + name);26 }27}