-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdifference.go
43 lines (38 loc) · 1.03 KB
/
difference.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package slices
import (
"reflect"
"github.com/golodash/godash/generals"
"github.com/golodash/godash/internal"
)
// Returns a slice of 'slice' elements that are not included in the
// other given slice using comparisons.
//
// Complexity: O(n*m)
//
// n = length of 'slice'
//
// m = length of 'notIncluded'
func Difference(slice, notIncluded interface{}) interface{} {
if !internal.SliceCheck(slice) {
panic("passed 'slice' variable is not slice type")
}
if !internal.SliceCheck(notIncluded) {
panic("passed 'notIncluded' variable is not slice type")
}
notInValue := reflect.ValueOf(notIncluded)
sliceValue := reflect.ValueOf(slice)
for i := sliceValue.Len() - 1; i > -1; i-- {
if i >= sliceValue.Len() {
continue
}
firstLoop:
for j := 0; j < notInValue.Len(); j++ {
if generals.Same(sliceValue.Index(i).Interface(), notInValue.Index(j).Interface()) {
sliceValue = reflect.AppendSlice(sliceValue.Slice(0, i), sliceValue.Slice(i+1, sliceValue.Len()))
i++
break firstLoop
}
}
}
return sliceValue.Interface()
}