Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Duplicate & DuplicateBy #122

Merged
merged 5 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
PR update
  • Loading branch information
CorentinClabaut committed Jul 4, 2022
commit e836eb4fca57a5bca65e3124a66a63b7dbca79af
66 changes: 44 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ Supported helpers for slices:
- Times
- Uniq
- UniqBy
- Duplicate
- DuplicateBy
- GroupBy
- Chunk
- PartitionBy
Expand Down Expand Up @@ -135,6 +133,10 @@ Supported search helpers:
- Find
- FindIndexOf
- FindLastIndexOf
- FindUniques
- FindUniquesBy
- FindDuplicates
- FindDuplicatesBy
- Min
- MinBy
- Max
Expand Down Expand Up @@ -337,26 +339,6 @@ uniqValues := lo.UniqBy[int, int]([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
// []int{0, 1, 2}
```

### Duplicate

Returns a slice with the first occurence of each duplicated elements of the collection. The order of result values is determined by the order they occur in the array.

```go
uniqValues := lo.Duplicate[int]([]int{1, 2, 2, 1, 2, 3})
// []int{1, 2}
```

### DuplicateBy

Returns a slice with the first occurence of each duplicated elements of the collection. The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is invoked for each element in array to generate the criterion by which uniqueness is computed.

```go
uniqValues := lo.UniqBy[int, int]([]int{3, 4, 5, 6, 7}, func(i int) int {
return i%3
})
// []int{3, 4}
```

### GroupBy

Returns an object composed of keys generated from the results of running each element of collection through iteratee.
Expand Down Expand Up @@ -1112,6 +1094,46 @@ str, index, ok := lo.FindLastIndexOf[string]([]string{"foobar"}, func(i string)
// "", -1, false
```

### FindUniques

Returns a slice with all the unique elements of the collection. The order of result values is determined by the order they occur in the array.

```go
uniqueValues := lo.FindUniques[int]([]int{1, 2, 2, 1, 2, 3})
// []int{3}
```

### FindUniquesBy

Returns a slice with all the unique elements of the collection. The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is invoked for each element in array to generate the criterion by which uniqueness is computed.

```go
uniqueValues := lo.FindUniquesBy[int, int]([]int{3, 4, 5, 6, 7}, func(i int) int {
return i%3
})
// []int{5}
```

### FindDuplicates

Returns a slice with the first occurence of each duplicated elements of the collection. The order of result values is determined by the order they occur in the array.

```go
duplicatedValues := lo.FindDuplicates[int]([]int{1, 2, 2, 1, 2, 3})
// []int{1, 2}
```

### FindDuplicatesBy

Returns a slice with the first occurence of each duplicated elements of the collection. The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is invoked for each element in array to generate the criterion by which uniqueness is computed.

```go
duplicatedValues := lo.FindDuplicatesBy[int, int]([]int{3, 4, 5, 6, 7}, func(i int) int {
return i%3
})
// []int{3, 4}
```

### Min

Search the minimum value of a collection.
Expand Down
112 changes: 112 additions & 0 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,118 @@ func FindOrElse[T any](collection []T, fallback T, predicate func(T) bool) T {
return fallback
}

// FindUniques returns a slice with all the unique elements of the collection.
// The order of result values is determined by the order they occur in the collection.
func FindUniques[T comparable](collection []T) []T {
isDupl := make(map[T]bool, len(collection))

for _, item := range collection {
duplicated, ok := isDupl[item]
if !ok {
isDupl[item] = false
} else if !duplicated {
isDupl[item] = true
}
}

result := make([]T, 0, len(collection)-len(isDupl))

for _, item := range collection {
if duplicated := isDupl[item]; !duplicated {
result = append(result, item)
}
}

return result
}

// FindUniquesBy returns a slice with all the unique elements of the collection.
// The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is
// invoked for each element in array to generate the criterion by which uniqueness is computed.
func FindUniquesBy[T any, U comparable](collection []T, iteratee func(T) U) []T {
isDupl := make(map[U]bool, len(collection))

for _, item := range collection {
key := iteratee(item)

duplicated, ok := isDupl[key]
if !ok {
isDupl[key] = false
} else if !duplicated {
isDupl[key] = true
}
}

result := make([]T, 0, len(collection)-len(isDupl))

for _, item := range collection {
key := iteratee(item)

if duplicated := isDupl[key]; !duplicated {
result = append(result, item)
}
}

return result
}

// FindDuplicates returns a slice with the first occurence of each duplicated elements of the collection.
// The order of result values is determined by the order they occur in the collection.
func FindDuplicates[T comparable](collection []T) []T {
isDupl := make(map[T]bool, len(collection))

for _, item := range collection {
duplicated, ok := isDupl[item]
if !ok {
isDupl[item] = false
} else if !duplicated {
isDupl[item] = true
}
}

result := make([]T, 0, len(collection)-len(isDupl))

for _, item := range collection {
if duplicated := isDupl[item]; duplicated {
result = append(result, item)
isDupl[item] = false
}
}

return result
}

// FindDuplicatesBy returns a slice with the first occurence of each duplicated elements of the collection.
// The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is
// invoked for each element in array to generate the criterion by which uniqueness is computed.
func FindDuplicatesBy[T any, U comparable](collection []T, iteratee func(T) U) []T {
isDupl := make(map[U]bool, len(collection))

for _, item := range collection {
key := iteratee(item)

duplicated, ok := isDupl[key]
if !ok {
isDupl[key] = false
} else if !duplicated {
isDupl[key] = true
}
}

result := make([]T, 0, len(collection)-len(isDupl))

for _, item := range collection {
key := iteratee(item)

if duplicated := isDupl[key]; duplicated {
result = append(result, item)
isDupl[key] = false
}
}

return result
}

// Min search the minimum value of a collection.
func Min[T constraints.Ordered](collection []T) T {
var min T
Expand Down
100 changes: 100 additions & 0 deletions find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,106 @@ func TestFindOrElse(t *testing.T) {
is.Equal(result2, "x")
}

func TestFindUniques(t *testing.T) {
is := assert.New(t)

result1 := FindUniques[int]([]int{1, 2, 3})

is.Equal(3, len(result1))
is.Equal([]int{1, 2, 3}, result1)

result2 := FindUniques[int]([]int{1, 2, 2, 3, 1, 2})

is.Equal(1, len(result2))
is.Equal([]int{3}, result2)

result3 := FindUniques[int]([]int{1, 2, 2, 1})

is.Equal(0, len(result3))
is.Equal([]int{}, result3)

result4 := FindUniques[int]([]int{})

is.Equal(0, len(result4))
is.Equal([]int{}, result4)
}

func TestFindUniquesBy(t *testing.T) {
is := assert.New(t)

result1 := FindUniquesBy[int, int]([]int{0, 1, 2}, func(i int) int {
return i % 3
})

is.Equal(3, len(result1))
is.Equal([]int{0, 1, 2}, result1)

result2 := FindUniquesBy[int, int]([]int{0, 1, 2, 3, 4}, func(i int) int {
return i % 3
})

is.Equal(1, len(result2))
is.Equal([]int{2}, result2)

result3 := FindUniquesBy[int, int]([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
return i % 3
})

is.Equal(0, len(result3))
is.Equal([]int{}, result3)

result4 := FindUniquesBy[int, int]([]int{}, func(i int) int {
return i % 3
})

is.Equal(0, len(result4))
is.Equal([]int{}, result4)
}

func TestFindDuplicates(t *testing.T) {
is := assert.New(t)

result1 := FindDuplicates[int]([]int{1, 2, 2, 1, 2, 3})

is.Equal(2, len(result1))
is.Equal([]int{1, 2}, result1)

result2 := FindDuplicates[int]([]int{1, 2, 3})

is.Equal(0, len(result2))
is.Equal([]int{}, result2)

result3 := FindDuplicates[int]([]int{})

is.Equal(0, len(result3))
is.Equal([]int{}, result3)
}

func TestFindDuplicatesBy(t *testing.T) {
is := assert.New(t)

result1 := FindDuplicatesBy[int, int]([]int{3, 4, 5, 6, 7}, func(i int) int {
return i % 3
})

is.Equal(2, len(result1))
is.Equal([]int{3, 4}, result1)

result2 := FindDuplicatesBy[int, int]([]int{0, 1, 2, 3, 4}, func(i int) int {
return i % 5
})

is.Equal(0, len(result2))
is.Equal([]int{}, result2)

result3 := FindDuplicatesBy[int, int]([]int{}, func(i int) int {
return i % 3
})

is.Equal(0, len(result3))
is.Equal([]int{}, result3)
}

func TestMin(t *testing.T) {
is := assert.New(t)

Expand Down
57 changes: 0 additions & 57 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,63 +123,6 @@ func UniqBy[T any, U comparable](collection []T, iteratee func(T) U) []T {
return result
}

// Duplicate returns a slice with the first occurence of each duplicated elements of the collection.
// The order of result values is determined by the order they occur in the collection.
func Duplicate[T comparable](collection []T) []T {
isDupl := make(map[T]bool, len(collection))

for _, item := range collection {
duplicated, ok := isDupl[item]
if !ok {
isDupl[item] = false
} else if !duplicated {
isDupl[item] = true
}
}

result := make([]T, 0, len(collection)-len(isDupl))

for _, item := range collection {
if duplicated := isDupl[item]; duplicated {
result = append(result, item)
isDupl[item] = false
}
}

return result
}

// DuplicateBy returns a slice with the first occurence of each duplicated elements of the collection.
// The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is
// invoked for each element in array to generate the criterion by which uniqueness is computed.
func DuplicateBy[T any, U comparable](collection []T, iteratee func(T) U) []T {
isDupl := make(map[U]bool, len(collection))

for _, item := range collection {
key := iteratee(item)

duplicated, ok := isDupl[key]
if !ok {
isDupl[key] = false
} else if !duplicated {
isDupl[key] = true
}
}

result := make([]T, 0, len(collection)-len(isDupl))

for _, item := range collection {
key := iteratee(item)

if duplicated := isDupl[key]; duplicated {
result = append(result, item)
isDupl[key] = false
}
}

return result
}

// GroupBy returns an object composed of keys generated from the results of running each element of collection through iteratee.
func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T {
result := map[U][]T{}
Expand Down
Loading