---
title: "Find duplicate YAML keys and their values in Go"
url: https://recursiveai.org/oneyaml/go/cookbook/list-duplicate-yaml-mapping-keys/
description: "Find repeated string keys in a YAML mapping with Go. Walk the parsed entries and print each occurrence’s value without choosing a winner."
version: "1.0.0"
---

# Find duplicate YAML keys and their values in Go

**oneyaml-go** is a free, open-source YAML library for Go from Recursive AI. MIT or Apache-2.0, no dependencies.

[pkg.go.dev](https://pkg.go.dev/oneyaml.dev/go) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-go) · [Docs](https://recursiveai.org/oneyaml/go/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/go/cookbook/list-duplicate-yaml-mapping-keys/list-duplicate-yaml-mapping-keys.zip)

Count string keys with `Entries()`, then print every value for keys that occur twice or more. Decoding into a Go map or struct fails on duplicate keys, so walk the entries to see both values.

```shell
go get oneyaml.dev/go
```

Requires Go 1.24 or later. Run in a Go module.

```go
package main

import (
    "fmt"
    "log"

    "oneyaml.dev/go" // Package name: oneyaml.
)

func main() {
    const src = `port: 80
name: web
port: 443
`
    doc, err := oneyaml.Parse([]byte(src))
    if err != nil {
        log.Fatal(err)
    }
    root := doc.Root()
    const strTag = "tag:yaml.org,2002:str"
    counts := map[string]int{}
    for key := range root.Entries() {
        if key.Tag() == strTag {
            counts[key.Value()]++
        }
    }
    for key, value := range root.Entries() {
        name := key.Value()
        if key.Tag() == strTag &&
            counts[name] > 1 {
            fmt.Printf("%s: %s\n",
                name, value.Source())
        }
    }
}
```

Paste the code above into main.go, run `go mod init example` and `go get oneyaml.dev/go`, then `go run .`; or unzip [the example](https://recursiveai.org/oneyaml/go/cookbook/list-duplicate-yaml-mapping-keys/list-duplicate-yaml-mapping-keys.zip) and `go run .`.

```text
port: 80
port: 443
```

If the file has a syntax error, `Parse` returns an error before the scan starts.

## Variations

**Show where each key occurs.** Replace the `fmt.Printf` call inside the second loop:

```go
line, col := key.Position()
fmt.Printf("%d:%d %s: %s\n",
    line, col, name, value.Source())
```

```text
1:1 port: 80
3:1 port: 443
```

## Related recipes

- [Locate a YAML value in the source](https://recursiveai.org/oneyaml/go/cookbook/locate-yaml-value-in-source/)
- [Read a YAML value supplied by a merge key](https://recursiveai.org/oneyaml/go/cookbook/read-effective-yaml-merge-value/)

---

## The short answer

Call `Parse` and range over `doc.Root().Entries()`, not a Go map of decoded values. Count the string keys in that mapping, then walk the entries again and print the value of every key whose count is greater than one. The second pass prints the first occurrence too, in the order the values appeared in the file.

## Can YAML have duplicate keys?

The [YAML specification](https://yaml.org/spec/1.2.2/#3211-nodes) requires keys to be unique within a mapping. A file can still contain two entries named `port`. oneyaml-go lets you parse that file so you can see what the author wrote, but `Unmarshal` rejects duplicates. `Get` and `Find` also refuse an ambiguous key with `ErrAmbiguousKey`; there’s no first- or last-value rule for that lookup. If the application needs one value, fix the input rather than interpreting the diagnostic output as a resolved configuration.

Uniqueness is local to each mapping. A `port` under `web` and a `port` under `db` don’t conflict. To check a nested mapping, run the same pair of loops on that mapping node with a fresh count map. The example checks the entries written at the root, not keys inherited through YAML merges.

## How do I find duplicate YAML keys without losing their values?

Compare keys as text after YAML removes the quotes and expands escapes. That’s what `key.Value()` does: `port` and `'port'` count as the same string key. The string tag check matters because a plain numeric key such as `80` and a quoted string key like `'80'` need not represent the same key. The recipe deliberately checks only string keys; it isn’t a general equality test for YAML keys of every type.

For the report, `value.Source()` gives the node’s text as written rather than converting it to a Go value. You can see whether one occurrence used quotes or a different representation. Keep the line and column if you need to send someone to both entries, especially when they are far apart. The example uses the key’s position so the location points to the repeated name, not to its value.
