There are many resources on how to remove duplicates and similar issues but I can\'t seem to be able to find any on removing unique elements. I\'m using SWI-Prolog but I don
As long as you don't know that the list is sorted in any way, and you want to keep the sequence of the non-unique elements, it seems to me you can't avoid making two passes: first count occurrences, then pick only repeating elements.
What if you use a (self-balancing?) binary tree for counting occurrences and look-up during the second pass? Definitely not O(n²), at least...