# Solution: Remove duplicates from ArrayList

**URL:** <https://discourse.processing.org/t/solution-remove-duplicates-from-arraylist/32742>\
**Category:** Processing\
**Created:** [October 11, 2021, 9:24am UTC](https://discourse.processing.org/t/solution-remove-duplicates-from-arraylist/32742 "2021-10-11T09:24:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ubuntu-in-a-nutshell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ubuntu-in-a-nutshell/32/16992_2.png) [@ubuntu-in-a-nutshell](https://discourse.processing.org/u/ubuntu-in-a-nutshell)\
**Post date:** [October 11, 2021, 9:24am UTC](https://discourse.processing.org/t/solution-remove-duplicates-from-arraylist/32742/1 "2021-10-11T09:24:23Z")

</div>

I have found a solution to [removing duplicates from ArrayList](https://forum.processing.org/one/topic/how-to-remove-duplicates-from-arraylist.html). I have built a custom function that returns an ArrayList and cleans up all duplicates. Here is my code for the custom function.

```auto
ArrayList removeDuplicates(ArrayList toCopy) {
  ArrayList finalResult = new ArrayList();
  for(int i = 0; i < toCopy.size(); i++) {
    if(!(finalResult.contains(toCopy.get(i)))) {
      finalResult.add(toCopy.get(i));
    }
  }
  return finalResult;
}

```

Say there is an string that contains “Hello World!”  
And the ArrayList is:  
0: “Hello”  
1: “World!”  
2: “Hello World!”  
3: “some item”  
4: “Hello World!”  
5: “Another item”  
And after you pass it through the function it will be:  
0: “Hello”  
1: “World!”  
2: “Hello World!”  
3: “some item”  
4: “Another item”  
Keeping only the first “Hello World”.  
Here is an simple example

```auto
ArrayList exampleal = new ArrayList();
void setup() {
  background(100);
  size(500, 500);
  exampleal.add("yay");
  exampleal.add("yay");
  exampleal.add("Item");
  exampleal.add("Another item");
  exampleal.add("Another item");

  println(exampleal.size());
  println(removeDuplicates(exampleal).size());
}
ArrayList removeDuplicates(ArrayList toCopy) {
  ArrayList finalResult = new ArrayList();
  for(int i = 0; i < toCopy.size(); i++) {
    if(!(finalResult.contains(toCopy.get(i)))) {
      finalResult.add(toCopy.get(i));
    }
  }
  return finalResult;
}

```

In the console where the println() is shown, you will see 5 then 3, because the first one is the original ArrayList, and the second one is the ArrayList where duplicates are deleted. There are two "yay"s and "Another item"s, so only the first ones are kept in the ArrayList.

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [October 11, 2021, 11:23am UTC](https://discourse.processing.org/t/solution-remove-duplicates-from-arraylist/32742/2 "2021-10-11T11:23:57Z")

</div>

Hi @ubuntu-in-a-nutshell ,

Wouldn’t it be easier if you used a Map Interface? Maps work in key, value pairs and don’t allow duplicate keys. You could easily transfer the ArrayList to a map, where you could assign the value has the frequency of the key appearing in the ArrayList!, if only need the keys without duplicate, just retrieve the keys 😉  
Here is an example:

> **[Convert ArrayList to HashMap in Java - GeeksforGeeks](https://www.geeksforgeeks.org/convert-arraylist-to-hashmap-in-java/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

And below you can find some documentation as well!

> **Map Documentation Links**
>
> [Map (Java Platform SE 8 )](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html)  
> [Map Interface in Java - GeeksforGeeks](https://www.geeksforgeeks.org/map-interface-java-examples/)

Best regards!

---

<div class="post-metadata">

**Author:** ![ubuntu-in-a-nutshell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ubuntu-in-a-nutshell/32/16992_2.png) [@ubuntu-in-a-nutshell](https://discourse.processing.org/u/ubuntu-in-a-nutshell)\
**Post date:** [October 11, 2021, 9:59pm UTC](https://discourse.processing.org/t/solution-remove-duplicates-from-arraylist/32742/3 "2021-10-11T21:59:05Z")

</div>

@MiguelSanches Yeah, I have heard of HashMaps. This post is just for anyone who wanted an ArrayList solution specifically.
