LeetCode: 217. 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.

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.

1// We can use a simple javascript object for our hashmap.
2
3var containsDuplicate = function(nums) {
4 var seen = {}
5
6 return false
7}
1// Typescript requires typing of input parameters.
2
3function containsDuplicate(nums: number[]): boolean {
4 var seen = {}
5
6 return false
7}
1// Dart also requires typing of input parameters.
2// In this case, List<int>.
3
4class Solution {
5 bool containsDuplicate(List<int> nums) {
6 var seen = {};
7
8 return false;
9 }
10}
1// Java also requires typing of input parameters.
2
3class Solution {
4 public boolean containsDuplicate(int[] nums) {
5 Map seen = new HashMap<>();
6
7 return false;
8 }
9}
1# Python doesn't require type of keywords such as var
2
3class Solution:
4 def containsDuplicate(self, nums: List[int]) -> bool:
5 seen = {}
6
7 return False
1// Although Go requires typing we infer the type using :=
2
3func containsDuplicate(nums []int) bool {
4 seen := make(map[int]bool)
5
6 return false
7}

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.

1// Javascript has a for of which helps us loop
2// each n in nums easily
3
4var containsDuplicate = function(nums) {
5 var seen = {}
6
7 for (var n of nums) {
8 if (seen[n]) {
9 return true
10 }
11 seen[n] = true
12 }
13 return false
14}
1//
2
3function containsDuplicate(nums: number[]): boolean {
4 var seen = {}
5
6 for (var n of nums) {
7 if (seen[n]) {
8 return true
9 }
10 seen[n] = true
11 }
12 return false
13}
1// Dart will throw an error if we don't explicitly
2// check seen[n] != null. Because null is not subtype of bool.
3
4class Solution {
5 bool containsDuplicate(List<int> nums) {
6 var seen = {};
7 for (var n in nums) {
8 if (seen[n] != null) {
9 return true;
10 } else {
11 seen[n] = true;
12 }
13 }
14 return false;
15 }
16}
1// In Java we can't check a hashmaps key's using the index like syntax of JS/TS.
2// We have to use .containsKey(value)
3
4class Solution {
5 public boolean containsDuplicate(int[] nums) {
6 Map seen = new HashMap<>();
7
8 for (int n : nums) {
9 if (seen.containsKey(n)) {
10 return true;
11 } else {
12 seen.put(n, true);
13 }
14 }
15
16 return false;
17 }
18}
1# In Python as well we have to safeguard.
2# Except we do so by using n in seen.
3
4class Solution:
5 def containsDuplicate(self, nums: List[int]) -> bool:
6 seen = {}
7
8 for n in nums:
9 if n in seen:
10 return True
11 else:
12 seen[n] = True
13 return False
1// In Go we don't have a for of loop but we can accomplish it using range
2// We also have a more verbose way of checking if a value is in our hashmap.
3
4func containsDuplicate(nums []int) bool {
5 seen := make(map[int]bool)
6 for _, n := range nums {
7 if _, ok := seen[n]; ok {
8 return true
9 } else {
10 seen[n] = true
11 }
12 }
13 return false
14}

Questions? Concerns?

Please comment a better solution if you have one.