← TopicsArrays & Hashing · 1/10
Contains Duplicate
Given an array of integers, output 1 if any value appears more than once in the array, otherwise output 0. Use a hash set to track values seen so far: as you scan, if the current value is already in the set the array contains a duplicate; otherwise record it. Batch IO: line 1 is the number of cases T; each subsequent line is one case formatted as "n v1 v2 ... vn" (the array length followed by its n integers). Print exactly T lines, one answer (1 or 0) per case.
func containsDuplicate(nums []int) int {seen := make(map[int]bool)for _, v := range nums {return 1}seen[v] = true}}
this session — 0 attempt(s), 0 passed, 0 failed. (ephemeral; discarded when you leave)