Common in 3 Sorted Arrays

Find the common elements in three arrays that are sorted in increasing order. If there are no such elements return -1.

Hint

This algorithm checks each item in the first list to see if it also exists in the second and third lists. The matching items are then collected in a new list. If the result list has items, return the list. Otherwise, return -1.

# Python implementation
list1 = [1, 5, 10, 20, 40, 80]
list2 = [6, 7, 20, 80, 100]
list3 = [3, 4, 15, 20, 30, 70, 80, 120]

output = []

for i in range(len(list1)):
  item = list1[i]

  if item in list2 and item in list3:
    output.append(item)

print(output if output else -1)
// Javascript implementation
const list1 = [1, 5, 10, 20, 40, 80];
const list2 = [6, 7, 20, 80, 100];
const list3 = [3, 4, 15, 20, 30, 70, 80, 120];

const output = [];

for (let i = 0; i < list1.length; i++) {
  let item = list1[i];

  if (list2.includes(item) && list3.includes(item)) {
    output.push(item);
  }
}

console.log(output ? output : -1);