Looks like uniq might be helpful, it appears built precisely like what you want.
https://www.ibm.com/docs/en/aix/7.2.0?topic=u-uniq-command
https://www.geeksforgeeks.org/linux-unix/uniq-command-in-linux-with-examples/
emacs
https://www.gnu.org/software/emacs/
https://susam.net/cc/mastering-emacs/ch05.html
M-x delete-duplicate-lines RET: Delete all but one copy of duplicate lines in region. When executed on the whole of the example buffer presented above, it leaves us with three non-empty lines and one blank line. When duplicate lines are encountered, the first instance of each line is kept intact and the others are deleted.
https://rants.org/2023/01/count-fold-lines
https://emacsredux.com/blog/2014/03/01/a-peek-at-emacs-24-dot-4-delete-duplicate-lines/