# Golang Program to find the Distinct elements from two arrays

In this tutorial, we will see to write a go language program to find distinct elements in two arrays. In this article we will write two programs. In the first program we will use the array of strings while in the second one we will use the array of integers.

## Algorithm

STEP 1 − import the fmt package.

STEP 2 − Define three functions named intersection(), uniquearr1 and uniquearr2.

STEP 3 − The intersection() finds the common array elements from the two arrays while the other two functions remove that common elements from given arrays.

STEP 4 − All these functions use for loop to iterate over the two arrays and check if the current element of one array is equal to the elements of the other.

STEP 5 − Start the main() function.

STEP 6 − Initialize two arrays of strings and store values to them.

STEP 7 − Call the intersection() function and store the final result in a different variable.

STEP 8 − Now, call the uniquearr1() and uniquearr2() by passing the arrays and the result array as arguments to it. store the result and append the two arrays in a new array.

STEP 9 − Print the result on the screen.

### Example 1

The following code illustrates how we can find the distinct elements in two different arrays of strings.

package main
import "fmt"
// function to get common elements
func intersection(arr1, arr2 []string) []string {
out := []string{}
bucket := map[string]bool{}
for _, i := range arr1 {
for _, j := range arr2 {
if i == j && !bucket[i] {
out = append(out, i)
bucket[i] = true
}
}
}
return out
}

// function to remove common elements from first array
func uniquearr1(arr1, result []string) []string {
index := len(arr1)
index1 := len(result)
for i := 0; i <= index-1; i++ {
for j := 0; j <= index1-1; j++ {
if arr1[i] == result[j] {
arr1[i] = arr1[index-1]
arr1[index-1] = ""
arr1 = arr1[:index-1]
index = index - 1
i = 0
}
}
}
return arr1
}
// function to remove common elements from second array
func uniquearr2(arr2, result []string) []string {
index1 := len(result)
lenarr2 := len(arr2)
for i := 0; i <= lenarr2-1; i++ {
for j := 0; j <= index1-1; j++ {
if arr2[i] == result[j] {
arr2[i] = arr2[lenarr2-1]
arr2[lenarr2-1] = ""
arr2 = arr2[:lenarr2-1]
lenarr2 = lenarr2 - 1
i = 0
}
}
}
return arr2
}
func main() {
arr1 := []string{"apple", "mango", "banana", "papaya"}
fmt.Println("The first array entered is:", arr1)
arr2 := []string{"cherry", "papaya", "mango"}
fmt.Println("The second array entered is:", arr2)
result := intersection(arr1, arr2)
fmt.Println()
result1 := uniquearr1(arr1, result)
result2 := uniquearr2(arr2, result)
var finalres []string
finalres = append(finalres, result1...)
finalres = append(finalres, result2...)
fmt.Println("The final array containing distinct values from the above mentioned arrays is:", finalres)
}


### Output

The first array entered is: [apple mango banana papaya]
The second array entered is: [cherry papaya mango]

The final array containing distinct values from the above mentioned arrays is: [apple banana cherry]


### Example 2

The following code illustrates how we can find the distinct elements in two different arrays of integers in go programming language.

package main
import "fmt"

// function to get common elements
func intersection(arr1, arr2 []int) []int {
out := []int{}
bucket := map[int]bool{}
for _, i := range arr1 {
for _, j := range arr2 {
if i == j && !bucket[i] {
out = append(out, i)
bucket[i] = true
}
}
}
return out
}
func uniquearr1(arr1, result []int) []int {
index := len(arr1)
index1 := len(result)
for i := 0; i <= index-1; i++ {
for j := 0; j <= index1-1; j++ {
if arr1[i] == result[j] {
arr1[i] = arr1[index-1]
arr1[index-1] = 0
arr1 = arr1[:index-1]
index = index - 1
i = 0
}
}
}
return arr1
}
func uniquearr2(arr2, result []int) []int {
index1 := len(result)
lenarr2 := len(arr2)
for i := 0; i <= lenarr2-1; i++ {
for j := 0; j <= index1-1; j++ {
if arr2[i] == result[j] {
arr2[i] = arr2[lenarr2-1]
arr2[lenarr2-1] = 0
arr2 = arr2[:lenarr2-1]
lenarr2 = lenarr2 - 1
i = 0
}
}
}
return arr2
}
func main() {
arr1 := []int{11, 25, 35, 23, 54}
fmt.Println("The first array entered is:", arr1)
arr2 := []int{35, 89, 60, 54, 23}
fmt.Println("The second array entered is:", arr2)
result := intersection(arr1, arr2)
fmt.Println()
result1 := uniquearr1(arr1, result)
result2 := uniquearr2(arr2, result)
var finalres []int
finalres = append(finalres, result1...)
finalres = append(finalres, result2...)
fmt.Println("The final array containing distinct values from the above mentioned arrays is:", finalres)
}


### Output

The first array entered is: [11 25 35 23 54]
The second array entered is: [35 89 60 54 23]

The final array containing distinct values from the above mentioned arrays is: [11 25 60 89]


## Conclusion

We have successfully compiled and executed a go language program to find the distinct elements of two arrays along with the examples.