Unique lines in an Emacs buffer

I needed to unique lines in a buffer’s content—the same functionality that shell command uniq provides—but could not find the proper Elisp equivalent for it, though. Even EmacsWiki suggests using the shell command to delete duplicate lines. Here goes my implementation for Lisp programs, it may come in useful.

Updated: regex quoting fixed.
Updated again: Aaron S. Hawley pointed me a much cleaner duplicate lines solution.

(defun uniq-lines (beg end)
  "Unique lines in region.
Called from a program, there are two arguments:
BEG and END (region to sort)."
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (goto-char (point-min))
      (while (not (eobp))
        (kill-line 1)
        (yank)
        (let ((next-line (point)))
          (while
              (re-search-forward
               (format "^%s" (regexp-quote (car kill-ring))) nil t)
            (replace-match "" nil nil))
          (goto-char next-line))))))