# Append elements to array avoiding duplicates

**URL:** <https://discuss.elastic.co/t/append-elements-to-array-avoiding-duplicates/325059>\
**Category:** Elasticsearch\
**Created:** [February 8, 2023, 7:35pm UTC](https://discuss.elastic.co/t/append-elements-to-array-avoiding-duplicates/325059 "2023-02-08T19:35:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergio\_Serra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergio_serra/32/116263_2.png) [@Sergio\_Serra](https://discuss.elastic.co/u/Sergio_Serra)\
**Post date:** [February 8, 2023, 7:35pm UTC](https://discuss.elastic.co/t/append-elements-to-array-avoiding-duplicates/325059/1 "2023-02-08T19:35:55Z")

</div>

Hello everyone,

Is there a performant way to append elements to an array avoiding duplicates basically making it work like a **Set** instead of a **List**?

This is the script i'm using, but this will duplicate entries in the array if the array already contains some of the elements. If i run it twice the index will contain [1,2,3,4,5,1,2,3,4,5] instead of [1,2,3,4,5]

```auto
"script": {
   "source": "ctx._source.ids.addAll(params.ids)",
   "params": {
     "ids": [1,2,3,4,5]
    }
} 

```

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 8, 2023, 7:36pm UTC](https://discuss.elastic.co/t/append-elements-to-array-avoiding-duplicates/325059/2 "2023-03-08T19:36:08Z")

</div>

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