# Help With Combining Lists in Hackathon Project

**URL:** <https://forum.code.org/t/help-with-combining-lists-in-hackathon-project/37590>\
**Category:** Coding and Debugging Help\
**Tags:** csp-unit-5\
**Created:** [February 3, 2023, 7:42am UTC](https://forum.code.org/t/help-with-combining-lists-in-hackathon-project/37590 "2023-02-03T07:42:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrB](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@MrB](https://forum.code.org/u/MrB)\
**Post date:** [February 3, 2023, 7:42am UTC](https://forum.code.org/t/help-with-combining-lists-in-hackathon-project/37590/1 "2023-02-03T07:42:00Z")

</div>

One of my students is making a project that has two lists. The lists share some values in common but not all and the student is trying to make a third list that only stores the elements they have in common. I want to find the simplest way possible for them to do this.

For example if list1 stores [a, b, c, d]  
And list2 stores [e, a, f, c]  
list3 should be [a, c]

I just want to find a very simple way to do this so my student can fully understand it. Thanks!

---

<div class="post-metadata">

**Author:** ![varrience](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/varrience/32/8042_2.png) [@varrience](https://forum.code.org/u/varrience)\
**Post date:** [February 3, 2023, 7:04pm UTC](https://forum.code.org/t/help-with-combining-lists-in-hackathon-project/37590/2 "2023-02-03T19:04:23Z")

</div>

Greetings @MrB,

I don’t know if this is the simplest solution but, I’ll show it first and then walk you through what i did

```auto
// Variable initialization
var list1 = ['a', 'b', 'c', 'd'];
var list2 = ['e', 'a', 'f', 'c'];
var list3 = [];
// Algorithm
for(var l1 = 0; l1 < list1.length; l1++) { // looks through 1st array
 for(var l2 = 0; l2 < list2.length; l2++) { // looks through the 2nd
  // this compares every element of list 2 to list 1 any that match get added
   if(list1[l1] == list2[l2]) {
    appendItem(list3, list1[l1]) // could also use list2[l2] in second parameter
   }
 }
}
// Result
console.log(list3)

```

I could have used .indexOf as well to spare your eyes from this but this is basically what that is doing much like you go down a shopping list to find anything missing this is basically an automated version of that and anything that matches will be added in list3 as the result

Hope this is what your looking for best of luck!

---

<div class="post-metadata">

**Author:** ![MrB](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@MrB](https://forum.code.org/u/MrB)\
**Post date:** [February 3, 2023, 8:22pm UTC](https://forum.code.org/t/help-with-combining-lists-in-hackathon-project/37590/3 "2023-02-03T20:22:48Z")

</div>

Thank you so much, this worked perfectly!
