# Removing duplicates from list, array

**URL:** <https://forum.code.org/t/removing-duplicates-from-list-array/35673>\
**Category:** CS Principles\
**Tags:** csp-unit-5\
**Created:** [January 8, 2022, 8:15pm UTC](https://forum.code.org/t/removing-duplicates-from-list-array/35673 "2022-01-08T20:15:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe\_padon](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/joe_padon/32/1377_2.png) [@joe\_padon](https://forum.code.org/u/joe_padon)\
**Post date:** [January 8, 2022, 8:15pm UTC](https://forum.code.org/t/removing-duplicates-from-list-array/35673/1 "2022-01-08T20:15:26Z")

</div>

I’m struggling with this. Can someone help me with coding a filtered list or updating a current list to remove duplicates?  
Example: var list = [“a”, “b”, “c”, “a”, “a”, “d”]  
What would be code to update list or create a new one that has  
list = [“a”, “b”, “c”, “d”] and remove the duplicates “a”

---

<div class="post-metadata">

**Author:** ![IMPixel](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/impixel/32/6744_2.png) [@IMPixel](https://forum.code.org/u/IMPixel)\
**Post date:** [January 9, 2022, 1:24am UTC](https://forum.code.org/t/removing-duplicates-from-list-array/35673/2 "2022-01-09T01:24:28Z")

</div>

1. Create a variable: newList and assign it to an empty list: 
2. Run a for loop through the list with the duplicates:  
for(var i = 0; i != list.length; i++){

}  
3. Here’s what you should be doing in that for loop:  
Check if the current list element (list[i]) is in newList. Here’s how you can check that: newList.indexOf(list[i]) != -1 . If this is true, then the value is in the list. Otherwise, its not. Then, create an if statement. If that expression is false, then you need to add that value into the list. Otherwise, dont do anything. newList is list now, but without duplicates

---

<div class="post-metadata">

**Author:** ![joe\_padon](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/joe_padon/32/1377_2.png) [@joe\_padon](https://forum.code.org/u/joe_padon)\
**Post date:** [January 9, 2022, 5:34am UTC](https://forum.code.org/t/removing-duplicates-from-list-array/35673/3 "2022-01-09T05:34:45Z")

</div>

Thank you so much! It works!

 ![Screen Shot 2022-01-08 at 10.33.49 PM](https://us1.discourse-cdn.com/flex016/uploads/codeorgforum/original/2X/2/220e409945653ec005f987ec183b4fb4cc71e1da.png)

---

<div class="post-metadata">

**Author:** ![IMPixel](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/impixel/32/6744_2.png) [@IMPixel](https://forum.code.org/u/IMPixel)\
**Post date:** [January 9, 2022, 8:20pm UTC](https://forum.code.org/t/removing-duplicates-from-list-array/35673/4 "2022-01-09T20:20:50Z")

</div>

Glad I could help, sorry for the few mistakes in my explanation. I was hurrying because I wouldn’t have access to internet very shortly after I had written the answer.
