# Remove duplicates from Array?

**URL:** <https://discourse.gohugo.io/t/remove-duplicates-from-array/18278>\
**Category:** support\
**Created:** [April 24, 2019, 10:03am UTC](https://discourse.gohugo.io/t/remove-duplicates-from-array/18278 "2019-04-24T10:03:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![OjunbamO](https://avatars.discourse-cdn.com/v4/letter/o/bbce88/32.png) [@OjunbamO](https://discourse.gohugo.io/u/OjunbamO)\
**Post date:** [April 24, 2019, 10:03am UTC](https://discourse.gohugo.io/t/remove-duplicates-from-array/18278/1 "2019-04-24T10:03:40Z")

</div>

Hi Guys,  
I think this topic was already answered, but I don’t get it. I want to loop through 2 ArrayLists, the Pages and then an JSON Array on the Markdown site:  
thats what i get:  
on page 1:

```auto
"metatags": [
    "Project",
    "Network",
    "Infrastructure"
],
on page 2:  
"metatags": [
    "todo",
    "organisation",
    "Infrastructure"
],

```

and then i add everything to an new ArrayList. Everything fine but how do i filter now for duplicates?  
The Navbar will show now:

```auto
    Project
    Network
    Infrastructure
    todo
    organisation
    Infrastructure

```

But how can I delete 1 Infrastructure from the list? I think I can do it with another arraylist, which is working like a whitelist?

I googled a lot now and I think I need to do it with `Scratch` and `isset`, here’s my code:

```auto
{{ range .Pages }}
  {{ range .Params.metatags }}
    {{ $scratch := newScratch }}

    {{ $allcategorys := slice .}}
    {{ range $allcategorys }}
      {{ if not ( isset ($scratch ) . ) }}
        {{ $scratch.Set "category" . }}
        {{ $scratch.Get . }}
      {{ else }}
      {{ end }}
    {{ end }}

    <li uk-filter-control='[data-tag~="{{ $scratch.Get "category" }}"]'>
      <a href="index.html#">{{ $scratch.Get "category"}}</a>
    </li>
  {{ end }}
{{ end }}

```

I hope somebody of you can help me. 🙂

---

<div class="post-metadata">

**Author:** ![HenrySkup](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/henryskup/32/8183_2.png) [@HenrySkup](https://discourse.gohugo.io/u/HenrySkup)\
**Post date:** [April 24, 2019, 11:24am UTC](https://discourse.gohugo.io/t/remove-duplicates-from-array/18278/2 "2019-04-24T11:24:07Z")

</div>

> **[union](https://gohugo.io/functions/union/)**
>
> Given two arrays or slices, returns a new array that contains the elements or objects that belong to either or both arrays/slices.

pseudocode (i.e. something like this)

```auto

{{/* Create a list of unique tags */}}

{{ $tags := (slice) }} 

{{ range .Pages }}
    {{ $tags = union $tags ( slice .Params.metatags ) }}
{{ end }}

{{/* Range over unique tags to make HTML */}}

{{ range .$tags }}
    {{/* HTML HERE */}}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![OjunbamO](https://avatars.discourse-cdn.com/v4/letter/o/bbce88/32.png) [@OjunbamO](https://discourse.gohugo.io/u/OjunbamO)\
**Post date:** [April 24, 2019, 11:29am UTC](https://discourse.gohugo.io/t/remove-duplicates-from-array/18278/3 "2019-04-24T11:29:34Z")

</div>

Thank You, thats a good solution but it’s the method with a “whitelist”. Do you have an solution with the opportunity to sort it without a “new Array”, just with the existing items in .Params.metatags?

---

<div class="post-metadata">

**Author:** ![HenrySkup](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/henryskup/32/8183_2.png) [@HenrySkup](https://discourse.gohugo.io/u/HenrySkup)\
**Post date:** [April 24, 2019, 11:36am UTC](https://discourse.gohugo.io/t/remove-duplicates-from-array/18278/4 "2019-04-24T11:36:26Z")

</div>

> **[uniq](https://gohugo.io/functions/uniq/)**
>
> Takes in a slice or array and returns a slice with subsequent duplicate elements removed.

This will take a slice (array) and remove duplicates. but I’m not sure exactly what you are asking/trying to do. are you trying to create the HTML as you range through the lists? this will be fairly convoluted, and you will need to create an array (this could be in .Scratch or in a Go variable) for this (to store the unique tags).

---

<div class="post-metadata">

**Author:** ![OjunbamO](https://avatars.discourse-cdn.com/v4/letter/o/bbce88/32.png) [@OjunbamO](https://discourse.gohugo.io/u/OjunbamO)\
**Post date:** [April 24, 2019, 12:06pm UTC](https://discourse.gohugo.io/t/remove-duplicates-from-array/18278/5 "2019-04-24T12:06:24Z")

</div>

The current problem is. That i have duplicated values in an Array. This duplicated values are coming from an Array in the Markdown file. In the end I want to have an normal navbar without any duplicates. The reason for the duplicates are that the Markdown files have different metatags and I wanna sort these with the Items in the Navbar. That is working fine but like I said couple of the items are duplicated because they have the same “metatag” like others for example “Infrastructure”.  
I want to import all Metatags from the different markdown files but they should display only once.

An image of the project here you can see what i mean:

 ![20](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/2X/2/2f255886d08671b2f0c1d5bc0f185138ce8d4924.png)

the problem on your first solution is, that I want to import all items by the Markdown Files and I won’t fill up the whitelist Array ($tags on your solution) with keywords. The Navbar should be filled up automatically.

The uniq Tag changes anything 😕 I tried to put it before different variables.

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [April 24, 2019, 2:27pm UTC](https://discourse.gohugo.io/t/remove-duplicates-from-array/18278/6 "2019-04-24T14:27:35Z")

</div>

Hi there. Try something like this. You can tweak it to your needs.

```nohighlight
{{ $categories := slice }}
{{ range .Pages }}
  {{ $categories = $categories | append .Params.metatags }}
{{ end }}
{{ $categories }} <!-- not unique -->
{{ $categories | uniq }} <!-- unique -->

```

Which will output:

```nohighlight
[Project Network Infrastructure todo organisation Infrastructure]
[Project Network Infrastructure todo organisation]

```

---

<div class="post-metadata">

**Author:** ![OjunbamO](https://avatars.discourse-cdn.com/v4/letter/o/bbce88/32.png) [@OjunbamO](https://discourse.gohugo.io/u/OjunbamO)\
**Post date:** [April 25, 2019, 6:25am UTC](https://discourse.gohugo.io/t/remove-duplicates-from-array/18278/7 "2019-04-25T06:25:30Z")

</div>

> [@zwbetz](#):
>
> {{ $categories := slice }} {{ range .Pages }} {{ $categories = $categories | append .Params.metatags }} {{ end }} {{ $categories }} {{ $categories | uniq }}

This worked for me perfect:

```
{{ $categories := slice }}
{{ range .Pages }}
  {{ $categories = $categories | append .Params.metatags }}
{{ end }}
{{ range ($categories | uniq) }}
  <li uk-filter-control='[data-tag~="{{ . }}"]'>
    <a href="index.html#">{{ . }}</a>
  </li>
{{ end }}

```

Thank You!
