Describe an algorithm to return the first occurrence of each unique element in a list, and then how you would return the k most frequent elements.
Assume the input is stored in a PostgreSQL table with one row per list element and a stable input order. Write a query that returns the first position for each distinct element, then a second result for the k most frequent elements.
| Column | Type | Description |
|---|---|---|
| positionPK | INT | 1-based position of the element in the input list |
| element_value | VARCHAR(100) | Value of the list element |