- Published on
217. Contains Duplicate
- Authors
- Name
- Loi Tran
- @PrimeTimeTrann
217. Contains Duplicate
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.
Solution
Loop through the array nums
.
Check if the number has been seen
before in a hashmap.
If so return true
, otherwise add it to a hashmap we use to keep track of seen
values.
Return false if the loop exits without returning.
Declare Hashmap
Declare hashmap, seen
, which is used to mark which numbers we've seen.
Return at the end of the function false
because if we exit the loop without returning, then we have no duplicate values.
// We can use a simple javascript object for our hashmap.
var containsDuplicate = function(nums) {
var seen = {}
return false
}
// Typescript requires typing of input parameters.
function containsDuplicate(nums: number[]): boolean {
var seen = {}
return false
}
// Dart also requires typing of input parameters.
// In this case, List<int>.
class Solution {
bool containsDuplicate(List<int> nums) {
var seen = {};
return false;
}
}
// Java also requires typing of input parameters.
class Solution {
public boolean containsDuplicate(int[] nums) {
Map seen = new HashMap<>();
return false;
}
}
# Python doesn't require type of keywords such as var
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
seen = {}
return False
// Although Go requires typing we infer the type using :=
func containsDuplicate(nums []int) bool {
seen := make(map[int]bool)
return false
}
Check if current number in hash map
Check if the hashmap contains the current number. If it does return true.
If it doesn't then add the number as a key in the hash map with a value of true.
// Javascript has a for of which helps us loop
// each n in nums easily
var containsDuplicate = function(nums) {
var seen = {}
for (var n of nums) {
if (seen[n]) {
return true
}
seen[n] = true
}
return false
}
//
function containsDuplicate(nums: number[]): boolean {
var seen = {}
for (var n of nums) {
if (seen[n]) {
return true
}
seen[n] = true
}
return false
}
// Dart will throw an error if we don't explicitly
// check seen[n] != null. Because null is not subtype of bool.
class Solution {
bool containsDuplicate(List<int> nums) {
var seen = {};
for (var n in nums) {
if (seen[n] != null) {
return true;
} else {
seen[n] = true;
}
}
return false;
}
}
// In Java we can't check a hashmaps key's using the index like syntax of JS/TS.
// We have to use .containsKey(value)
class Solution {
public boolean containsDuplicate(int[] nums) {
Map seen = new HashMap<>();
for (int n : nums) {
if (seen.containsKey(n)) {
return true;
} else {
seen.put(n, true);
}
}
return false;
}
}
# In Python as well we have to safeguard.
# Except we do so by using n in seen.
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
seen = {}
for n in nums:
if n in seen:
return True
else:
seen[n] = True
return False
// In Go we don't have a for of loop but we can accomplish it using range
// We also have a more verbose way of checking if a value is in our hashmap.
func containsDuplicate(nums []int) bool {
seen := make(map[int]bool)
for _, n := range nums {
if _, ok := seen[n]; ok {
return true
} else {
seen[n] = true
}
}
return false
}
Questions? Concerns?
Please comment a better solution if you have one.