- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Golang Program to find the common elements from two arrays
- Golang Program to find the uncommon elements from two arrays
- C++ Program to find the common elements from two arrays
- Program to find maximum product of two distinct elements from an array in Python
- Program to find uncommon elements in two arrays - JavaScript
- Java Program to Find Common Elements Between Two Arrays
- Program to find the largest product of two distinct elements in Python
- Golang program to find intersection of two sorted arrays using two pointer approach
- Java Program to Find the closest pair from two sorted arrays
- JavaScript Program for find common elements in two sorted arrays
- Golang Program To Iterate Over Each Element From The Arrays
- Program to find the length of longest substring which has two distinct elements in Python
- C++ program to find two numbers from two arrays whose sum is not present in both arrays
- Print uncommon elements from two sorted arrays
- Golang Program to Add Two Matrix Using Multi-dimensional Arrays
