Reman
Reman

Reputation: 8119

Remove duplicates from a list (in vim)

This is my list:

['02', '03', '03', '16', '17', '17', '28', '29', '29']

I would like to know how I can remove the duplicates from this list.

Would it be also possible when I add an item to the list to check if the item is already in the list (to avoid duplicates?)

Upvotes: 8

Views: 3044

Answers (4)

Hotschke
Hotschke

Reputation: 10200

Vim 7.4.218 (25 Mar 2014) and newer

provides

uniq({list} [, {func} [, {dict}]])          *uniq()* *E882*
        Remove second and succeeding copies of repeated adjacent
        {list} items in-place.  Returns {list}.  If you want a list
        to remain unmodified make a copy first: >
            :let newlist = uniq(copy(mylist))
<       The default compare function uses the string representation of
        each item.  For the use of {func} and {dict} see |sort()|.

Upvotes: 4

mMontu
mMontu

Reputation: 9273

You could also convert the list to the keys in a Dictionary:

let list=['02', '03', '03', '16', '17', '17', '28', '29', '29']
let dict = {}
for l in list
   let dict[l] = ''
endfor
let uniqueList = keys(dict)

This works for both sorted and unsorted lists.

Upvotes: 3

odrm
odrm

Reputation: 5259

This ex-mode (i.e. enter after :) regex substitution will eliminate the duplicates (provided they are consecutive):

s/\('[0-9]\+'\),\s\+\1/\1/g

Upvotes: 4

ZyX
ZyX

Reputation: 53644

Try

let list=['02', '03', '03', '16', '17', '17', '28', '29', '29']
let unduplist=filter(copy(list), 'index(list, v:val, v:key+1)==-1')

. For the second question, see :h index().

By the way, if

  1. all list items are strings;
  2. there is no empty strings possible;
  3. you don't care about order of list items

then you should probably use a Dictionary instead: for large number of strings searching for duplicates is faster (and really not required).

Upvotes: 15

Related Questions