How do I remove duplicate characters and keep the unique one only in Perl?

前端 未结 11 760
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 16:08

How do I remove duplicate characters and keep the unique one only. For example, my input is:

EFUAHUU
UUUEUUUUH
UJUJHHACDEFUCU

Expected out

11条回答
  •  长情又很酷
    2020-12-05 16:38

    Here is a solution, that I think should work faster than the lookahead one, but is not regexp-based and uses hashtable.

    perl -n -e '%seen=();' -e 'for (split //) {print unless $seen{$_}++;}' 
    

    It splits every line into characters and prints only the first appearance by counting appearances inside %seen hashtable

提交回复
热议问题