# Array.filter() delete

**URL:** <https://forum.dfinity.org/t/array-filter-delete/15676>\
**Category:** Language Support\
**Tags:** Motoko\
**Created:** [September 29, 2022, 5:38am UTC](https://forum.dfinity.org/t/array-filter-delete/15676 "2022-09-29T05:38:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rncrmn](https://avatars.discourse-cdn.com/v4/letter/r/e480ec/32.png) [@rncrmn](https://forum.dfinity.org/u/rncrmn)\
**Post date:** [September 29, 2022, 5:38am UTC](https://forum.dfinity.org/t/array-filter-delete/15676/1 "2022-09-29T05:38:01Z")

</div>

Hello everyone,

I created a simple note app for learning purposes.

I use stable var array to save the data, now I’m stuck on how to remove the specific data from the array.

Please see the screenshot below.

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/1/199cc7aab4efde43452509b3a045a69a5a6018ff.png)

---

<div class="post-metadata">

**Author:** ![rncrmn](https://avatars.discourse-cdn.com/v4/letter/r/e480ec/32.png) [@rncrmn](https://forum.dfinity.org/u/rncrmn)\
**Post date:** [September 29, 2022, 5:56am UTC](https://forum.dfinity.org/t/array-filter-delete/15676/2 "2022-09-29T05:56:55Z")

</div>

It’s me again, I able to make it work now (see screenshot below).

Please let me know if you know any better solutions (I’m not 100% with my code), I really appreciate it.

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/5/5f1eecf8552b8ffa2b6192d82adf17f76e225098.png)

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [September 29, 2022, 8:30am UTC](https://forum.dfinity.org/t/array-filter-delete/15676/3 "2022-09-29T08:30:38Z")

</div>

There are ways to improve this code, but the real reply should be: if you need to add or remove elements regularly, then **do not use arrays**! That always requires a full copy of the array (your code even performs 3 copies), which is very expensive and leads to quadratic cost.

What you want here is a map data structure, e.g., an RBMap with note ids as keys. Then you can just call the delete function on it.

---

<div class="post-metadata">

**Author:** ![infu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/infu/32/11321_2.png) [@infu](https://forum.dfinity.org/u/infu)\
**Post date:** [September 29, 2022, 8:55am UTC](https://forum.dfinity.org/t/array-filter-delete/15676/4 "2022-09-29T08:55:59Z")

</div>

You can also use var Array with null defaults. Instead of deleting elements, you will just set them to null.

```auto
let items : [var ?Text] = Array.init<?Text>(1000, null); 

```

You can go up to ~5mil elements if you need.

---

<div class="post-metadata">

**Author:** ![rncrmn](https://avatars.discourse-cdn.com/v4/letter/r/e480ec/32.png) [@rncrmn](https://forum.dfinity.org/u/rncrmn)\
**Post date:** [September 29, 2022, 1:17pm UTC](https://forum.dfinity.org/t/array-filter-delete/15676/5 "2022-09-29T13:17:09Z")

</div>

@rossberg I didn’t know that, I learned something new from your comment, thank you for that, I really appreciate it.

---

<div class="post-metadata">

**Author:** ![rncrmn](https://avatars.discourse-cdn.com/v4/letter/r/e480ec/32.png) [@rncrmn](https://forum.dfinity.org/u/rncrmn)\
**Post date:** [September 29, 2022, 1:19pm UTC](https://forum.dfinity.org/t/array-filter-delete/15676/6 "2022-09-29T13:19:35Z")

</div>

@infu wow, this is a clean code compared to what I did, thank you so much for this.
