# Detecting duplicate items in an array

**URL:** <https://forum.ansible.com/t/detecting-duplicate-items-in-an-array/39697>\
**Category:** Get Help\
**Created:** [January 18, 2025, 8:18pm UTC](https://forum.ansible.com/t/detecting-duplicate-items-in-an-array/39697 "2025-01-18T20:18:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [January 18, 2025, 8:18pm UTC](https://forum.ansible.com/t/detecting-duplicate-items-in-an-array/39697/1 "2025-01-18T20:18:54Z")

</div>

Checking if a list of items has any duplicates can be done using [ansible.builtin.unique filter](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/unique_filter.html), for example:

```yaml
- name: Define a list with duplicates
  ansible.builtin.set_fact:
    list:
      - foo
      - bar
      - bar
      - baz

- name: Check if the list has duplicates
 ansible.builtin.assert:
   that:
     - ( list | length ) == ( list | unique | length )

```

However I haven’t found a filter that returns the duplicated items, this is the best I have come up with:

```yaml
- name: Check if the list has duplicates
  ansible.builtin.assert:
    that:
      - item != ansible_loop.previtem
    fail_msg: >-
      Unique list items are required but {{ item }} is a dupe
  when:
    - ansible_loop.previtem is defined
    - ( list | unique | length ) != ( list | length )
  loop: "{{ list | sort }}"
  loop_control:
    extended: true

```

I have also found another way to [get duplicate items from list](https://stackoverflow.com/a/60829821) that @vbotka came up with, if there isn’t a filter for this perhaps one is needed?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 18, 2025, 10:08pm UTC](https://forum.ansible.com/t/detecting-duplicate-items-in-an-array/39697/2 "2025-01-18T22:08:48Z")

</div>

For the record, the solution posted in the above link is below. Given the list

```yaml
  l: [foo, bar, bar, baz]

```

the declaration

```yaml
  r: "{{ l | community.general.counter
           | dict2items
           | selectattr('value', '>', 1)
           | map(attribute='key') }}"

```

gives

```yaml
  r: [bar]

```

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [January 19, 2025, 9:48am UTC](https://forum.ansible.com/t/detecting-duplicate-items-in-an-array/39697/3 "2025-01-19T09:48:40Z")

</div>

Thanks @vbotka that is a far more elegant solution that mine, sorry I didn’t appreciate this from the Stackoverflow post 🙄 .

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 18, 2025, 9:49am UTC](https://forum.ansible.com/t/detecting-duplicate-items-in-an-array/39697/4 "2025-02-18T09:49:20Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
