我正在使用它来查找列表重复项:
(defun have-dups (x)
(let ((dups (copy-tree x)))
(if (eq (length (delete-dups dups)) (length x))
nil
t)))
(have-dups (list 1 2 3 3)) ;=> t
(have-dups (list 1 2 3)) ;=> nil
考虑到 copy-tree
和 delete-dups
的开销,可能有更好的方法。
最佳答案
使用哈希表,一旦你发现一个元素已经存在于哈希表中,你就知道你有重复项:
(defun has-dup (list)
(block nil
(let ((hash (make-hash-table :test 'eql)))
(map ()
(lambda (item)
(if (gethash item hash)
(return t)
(setf (gethash item hash) t)))
list))))
关于list - Elisp:如何查找列表重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49005589/