reliqarts/go-common

View on GitHub
strings.go

Summary

Maintainability
A
0 mins
Test Coverage
package common

// Intersection finds the intersection between the two given slices.
func Intersection(s1, s2 []string) (inter []string) {
    hash := make(map[string]bool)
    for _, e := range s1 {
        hash[e] = true
    }
    for _, e := range s2 {
        // If elements present in the hashmap then append intersection list.
        if hash[e] {
            inter = append(inter, e)
        }
    }
    //Remove duplicates from slice.
    inter = RemoveDuplicates(inter)
    return
}

// RemoveDuplicates remove duplicates from slice.
func RemoveDuplicates(elements []string) (nodups []string) {
    encountered := make(map[string]bool)
    for _, element := range elements {
        if !encountered[element] {
            nodups = append(nodups, element)
            encountered[element] = true
        }
    }
    return
}