From 2d7945696c9bdbac8cf3f850141d70adf11cc8d9 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 22 Jul 2024 11:57:53 +0300 Subject: [PATCH] exslices: add utility to deduplicate unsorted list --- exslices/deduplicate.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 exslices/deduplicate.go diff --git a/exslices/deduplicate.go b/exslices/deduplicate.go new file mode 100644 index 0000000..1226c73 --- /dev/null +++ b/exslices/deduplicate.go @@ -0,0 +1,23 @@ +// Copyright (c) 2024 Tulir Asokan +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package exslices + +// DeduplicateUnsorted removes duplicates from the given slice without requiring that the input slice is sorted. +// The order of the output will be the same as the input. +// +// If you don't care about the order of the output, it's recommended to sort the list and then use [slices.Compact]. +func DeduplicateUnsorted[T comparable](s []T) []T { + seen := make(map[T]struct{}, len(s)) + result := make([]T, 0, len(s)) + for _, item := range s { + if _, ok := seen[item]; !ok { + seen[item] = struct{}{} + result = append(result, item) + } + } + return result +}