summaryrefslogtreecommitdiff
path: root/dupltxref.go
diff options
context:
space:
mode:
Diffstat (limited to 'dupltxref.go')
-rw-r--r--dupltxref.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/dupltxref.go b/dupltxref.go
new file mode 100644
index 0000000..da5b597
--- /dev/null
+++ b/dupltxref.go
@@ -0,0 +1,35 @@
+package badtudexo
+
+// This finds a list of duplicate entries, including the index of the
+// duplicate, and its value, in a given row. Note that it does not return both
+// entries as duplicates, only the second. But that should be good enough for
+// now.
+
+type DuplicateEntry[T any] struct {
+ Value T
+ Index int
+}
+
+func DuplicateNew[T any](value T, index int) DuplicateEntry[T] {
+ return DuplicateEntry[T] { value, index }
+}
+
+// O(N^2) algorithm: iterates through the list for each value, then again to
+// find if they are in it up to that point. Hence this does not scale well.
+func Duplicate[T comparable](values []T) []DuplicateEntry[T] {
+ var dupls []DuplicateEntry[T]
+ for i, v := range values {
+ for ii, iv := range values {
+ // Only iterate to before the current index.
+ if ii >= i { break }
+
+ if v == iv {
+ // We have a duplicate!
+ dupls = append(dupls, DuplicateNew(v, i))
+ break
+ }
+ }
+ }
+
+ return dupls
+}