From 79ea04f9d334eb15a8bca065b8d32a2efd8a6f89 Mon Sep 17 00:00:00 2001 From: ductnn Date: Mon, 25 Mar 2024 09:53:52 +0700 Subject: [PATCH] add sol --- .../FindAllDuplicatesinanArray.go | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 leetcode/442.FindAllDuplicatesinanArray/FindAllDuplicatesinanArray.go diff --git a/leetcode/442.FindAllDuplicatesinanArray/FindAllDuplicatesinanArray.go b/leetcode/442.FindAllDuplicatesinanArray/FindAllDuplicatesinanArray.go new file mode 100644 index 0000000..937a051 --- /dev/null +++ b/leetcode/442.FindAllDuplicatesinanArray/FindAllDuplicatesinanArray.go @@ -0,0 +1,28 @@ +// https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ + +package main + +import "fmt" + +func findDuplicates(nums []int) []int { + cnt := make(map[int]int) + res := []int{} + + for _, num := range nums { + cnt[num]++ + // fmt.Println(cnt) + } + + for num, count := range cnt { + if count > 1 { + res = append(res, num) + } + } + + return res +} + +func main() { + nums := []int{4, 3, 2, 7, 8, 2, 3, 1} + fmt.Println(findDuplicates(nums)) +}