Junior — Middle
How can you exclude duplicate elements from returned data?
sobes.tech AI
Answer from AI
To exclude duplicate elements from the returned data, data structures that do not allow duplicates, such as sets, are usually used. Different programming languages have built-in types or libraries for working with sets.
Example in Python:
items = [1, 2, 2, 3, 4, 4, 5]
unique_items = list(set(items)) # [1, 2, 3, 4, 5]
If data is returned from a database, you can use the DISTINCT keyword in an SQL query:
SELECT DISTINCT column_name FROM table_name;
You can also filter duplicates programmatically, for example, by iterating through a list and adding elements to a new list only if they are not already there.