Avatar ·

How to get all unique values from an array in JavaScript (remove duplicates)

Hello everyone! I'm a beginner in JavaScript and ran into a problem while working with arrays. I have an array that contains duplicate values, and I need to keep only unique elements. For example, I have this array: const fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'kiwi', 'orange']; I want to get as output: ['apple', 'banana', 'orange', 'kiwi'] (order doesn't matter, the main thing is to remove duplicates). I tried using a simple for loop and checking if the element already exists in the new array, but it seems too cumbersome. Maybe there is a more elegant solution? I've heard about Set, but I don't quite understand how to use it. Also, I'm interested in which method is considered the fastest for large arrays (e.g., 10,000+ elements). And also: can this be done