mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-06-05 04:30:16 +02:00
Merge pull request #8750 from Chinwendu20/fnd
fn: Added `HasDuplicates` function to slice
This commit is contained in:
commit
68ea8d5312
@ -198,3 +198,10 @@ func Sum[B Number](items []B) B {
|
|||||||
return a + b
|
return a + b
|
||||||
}, 0, items)
|
}, 0, items)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HasDuplicates checks if the given slice contains any duplicate elements.
|
||||||
|
// It returns false if there are no duplicates in the slice (i.e., all elements
|
||||||
|
// are unique), otherwise returns false.
|
||||||
|
func HasDuplicates[A comparable](items []A) bool {
|
||||||
|
return len(NewSet(items...)) != len(items)
|
||||||
|
}
|
||||||
|
@ -241,3 +241,43 @@ func TestSliceToMap(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestHasDuplicates tests the HasDuplicates function.
|
||||||
|
func TestHasDuplicates(t *testing.T) {
|
||||||
|
// Define test cases.
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
items []int
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "All unique",
|
||||||
|
items: []int{1, 2, 3, 4, 5},
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Some duplicates",
|
||||||
|
items: []int{1, 2, 2, 3, 4},
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "No items",
|
||||||
|
items: []int{},
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "All duplicates",
|
||||||
|
items: []int{1, 1, 1, 1},
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute each test case.
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got := HasDuplicates(tc.items)
|
||||||
|
|
||||||
|
require.Equal(t, tc.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user