Avatar ·

Removing duplicates from an array in Swift: best ways

Hi everyone! I'm new to Swift and ran into a problem: I need to remove duplicates from an array of strings. I have an array:\n\n```swift\nlet fruits = ["apple", "banana", "apple", "orange", "banana", "kiwi"]\n```\n\nI want to get the output: `["apple", "banana", "orange", "kiwi"]` — that is, without repetitions. I tried using `Set`, but it doesn't preserve the order of elements:\n\n```swift\nlet uniqueFruits = Array(Set(fruits))\n// Result may be: ["banana", "kiwi", "orange", "apple"]\n```\n\nOrder is important to me because the array is displayed in a table. I also saw a solution with `filter` and `contains`, but it seems slow for large arrays.\n\nTell me, how to properly remove duplicates from an array in Swift while preserving the order of elements? Are there built-in methods or best practices for this?