Remove unique elements only

后端 未结 2 860
礼貌的吻别
礼貌的吻别 2021-01-19 09:46

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

2条回答
  •  有刺的猬
    2021-01-19 10:17

    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...

提交回复
热议问题