Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- )
- // Define your struct
- type Person struct {
- Name string
- Age int
- City string
- }
- func main() {
- // Create an array of structs
- people := []Person{
- {"John", 25, "New York"},
- {"Jane", 30, "London"},
- {"John", 30, "New York"},
- {"Bob", 22, "Paris"},
- {"Jane", 30, "London"},
- }
- // Create a map to store the encountered values
- seen := make(map[struct{ Name, City string }]bool)
- // Iterate through the array
- for _, person := range people {
- // Check if the combination of Name and City has been seen before
- key := struct{ Name, City string }{person.Name, person.City}
- if seen[key] {
- fmt.Printf("Duplicate found: %+v\n", person)
- } else {
- // Mark the current combination as seen
- seen[key] = true
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement