Sanjeet Kunwar
Sanjeet Kunwar

Reputation: 103

go program to compare two arrays and store the matching elements in third one in golang

Hi i am new in go and i just write a program to compare two arrays and stored the matching element in third one but it is not sucessfully executed


package main

import (
    "fmt"
)

func main(){

    var firstarray =[10] int {1,9,12,16,13,11,14,18,8,12}
     var secondarray =[10] int {9,7,15,8,14,19,34,18,13,11}
    var finalarray [] int

     var i,j,mat int

     for i=0; i<=10; i++{

        for j=0; j<=10; j++{

            if firstarray[i] == secondarray[j]{
                finalarray[mat] == secondarray[j]
                mat++
            }
        }
     }

    fmt.Println("matching no is",finalarray)

}

Upvotes: 0

Views: 3157

Answers (3)

hongbshi
hongbshi

Reputation: 11

package main

import (
    "fmt"
)

func main() {
    type void struct{}
    var member void
    firstarray := map[int]void{1: member, 9: member, 12: member, 16: member, 13: member, 11: member, 14: member, 18: member, 8: member}
    secondarray := map[int]void{9: member, 7: member, 15: member, 8: member, 14: member, 19: member, 34: member, 18: member, 13: member, 11: member}

    var finalarray []int
    for k := range firstarray {
        if _, ok := secondarray[k]; ok {
            finalarray = append(finalarray, k)
        }
    }

    fmt.Println("matching no is", finalarray)
}

Upvotes: 1

saddam
saddam

Reputation: 829

Actually! to insert into third array there is Index out of range error was generating, So first of all you've to give an index length so that it can point the area into array where you want to store the value.

Also to assign value into third variable you've to use single assignment operator(=) rather than equal(==) operator.

Follow the below example.

package main

import (
    "fmt"
)

func main(){

    var firstarray =[10] int {1,9,12,16,13,11,14,18,8,12}
     var secondarray =[10] int {9,7,15,8,14,19,34,18,13,11}
    var finalarray [10]int

     var i,j,mat int

     for i=0; i<10; i++{

        for j=0; j<10; j++{

            if firstarray[i] == secondarray[j]{
                finalarray[mat] = secondarray[j]
                mat++
            }
        }
     }

    fmt.Println("matching no is",finalarray)

}

Upvotes: 1

Paul Hankin
Paul Hankin

Reputation: 58271

The main errors in your code are accessing arrays out-of-bounds (which you can fix by iterating over the elements in the arrays rather than using indexes), and the construction of the final array (which you can build up using append, rather than assigning to indexes).

You also use == to assign, which is a mistake since it's a comparison operator. Single equals = is the assignment operator.

Here's a cleaned-up, working version of your code.

package main

import (
    "fmt"
)

func main() {
    firstarray := []int{1, 9, 12, 16, 13, 11, 14, 18, 8, 12}
    secondarray := []int{9, 7, 15, 8, 14, 19, 34, 18, 13, 11}
    var finalarray []int

    for _, a1 := range firstarray {
        for _, a2 := range secondarray {
            if a1 == a2 {
                finalarray = append(finalarray, a2)
            }
        }
    }

    fmt.Println("matching no is", finalarray)

}

It would be better to use maps to find the matching elements, since then the code can be O(n) rather than O(n^2).

Upvotes: 1

Related Questions