Version 1 of Unique Element List

Updated 2010-07-16 20:51:13 by AMG

CJB: Returns the list without duplicate elements. This is different from [lsort -unique] in that it maintains the list ordering. It also keeps the first element encountered and removes subsequent callings.

proc uniqueList {list} {
  set new {}
  foreach item $list {
    if {[lsearch $new $item] < 0} {
      lappend new $item
    }
  }
  return $new
}

AMG: Here's another way which internally uses a hash table (amortized constant time per element, linear time overall) instead of a linear search (linear time per element, quadratic time overall):

proc uniqueList2 {list} {
  set dict {}
  foreach item $list {
    dict set dict $item ""
  }
  dict keys $dict
}

Performance timing:

set test [split "this is my test data" ""]
llength $test                                    ;#    20 elements
time {uniqueList  $test} 100000                  ;#    20.78 microseconds per iteration
time {uniqueList2 $test} 100000                  ;#    13.6  microseconds per iteration
set test [lrepeat 1000 {*}$test]; list
llength $test                                    ;# 20000 elements
time {uniqueList  $test} 1000                    ;# 17562.0 microseconds per iteration
time {uniqueList2 $test} 1000                    ;#  6844.0 microseconds per iteration