瀏覽代碼

Simplify removeDuplicates

Use a sort+compact construct instead of doing it by hand with a hashmap. The
time complexity is now O(nlogn+n) instead of O(n), and space complexity around
O(logn) instead of O(n+uniq(n)), but it shouldn't matter anyway, since
removeDuplicates is only called to deduplicate tags.
jvoisin 2 年之前
父節點
當前提交
863a5b3648
共有 1 個文件被更改,包括 4 次插入11 次删除
  1. 4 11
      internal/storage/entry.go

+ 4 - 11
internal/storage/entry.go

@@ -8,6 +8,7 @@ import (
 	"errors"
 	"fmt"
 	"log/slog"
+	"slices"
 	"time"
 
 	"miniflux.app/v2/internal/crypto"
@@ -615,15 +616,7 @@ func (s *Storage) UnshareEntry(userID int64, entryID int64) (err error) {
 	return
 }
 
-// removeDuplicate removes duplicate entries from a slice
-func removeDuplicates[T string | int](sliceList []T) []T {
-	allKeys := make(map[T]bool)
-	list := []T{}
-	for _, item := range sliceList {
-		if _, value := allKeys[item]; !value {
-			allKeys[item] = true
-			list = append(list, item)
-		}
-	}
-	return list
+func removeDuplicates(l []string) []string {
+	slices.Sort(l)
+	return slices.Compact(l)
 }