-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContainsDuplicate.java
44 lines (34 loc) · 1.07 KB
/
ContainsDuplicate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Contains Duplicate - https://leetcode.com/problems/contains-duplicate/description/
// Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
// Example 1:
// Input: nums = [1,2,3,1]
// Output: true
// Example 2:
// Input: nums = [1,2,3,4]
// Output: false
package Questions.Easy;
import java.util.Arrays;
import java.util.HashMap;
public class ContainsDuplicate {
// approch 1 - TC O(nlogn) , SC O(1)
static boolean containsDup1(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
}
// apprch 2 - TC O(n) , SC O(n)
static boolean containsDup2(int[] nums) {
HashMap<Integer,Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
return true;
}
map.put(nums[i],1);
}
return false;
}
}