Find non-duplicated items in list

Imagine this: you have a list of many items you bought on supermarket and you want to know which ones are not duplicated.

With indexOf and lastIndexOf, we can do that easily. indexOf will return the first occurrence of the item in the array. lastIndexOf will return the item's last position of the last occurrence.

const list = ["javascript", "php", "mysql", "javascript", "php", "javascript"]; list.indexOf("javascript"); // 0 list.lastIndexOf("javascript"); //5 list.indexOf("php"); // 1 list.lastIndexOf("php"); //4 list.indexOf("mysql"); // 2 list.lastIndexOf("mysql"); //2

When indexOf == lastIndexOf means the item is unique. So, with that, we can create a function that will return an array with non-duplicated items:

function nomDuplicatedItems(arr) { if (arr.length == 0) return []; if (arr.length == 1) return arr[0]; const nomduplicated = []; for (let i = 0; i < arr.length; i++) { if (arr.indexOf(arr[i]) == arr.lastIndexOf(arr[i])) nomduplicated.push(arr[i]); } return nomduplicated; } const list = ["javascript", "php", "mysql", "javascript", "php", "javascript"]; nomDuplicatedItems(list); // ["mysql"]