Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: uniq'ing arrays Message-ID: <10501@jpl-devvax.JPL.NASA.GOV> Date: 21 Nov 90 18:02:46 GMT References: <1990Nov21.021344.16038@fxgrp.fx.com> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 25 In article <1990Nov21.021344.16038@fxgrp.fx.com> grady@postgres.berkeley.edu writes: : grep is a useful unix utility that has a parallel perl operator. : What about uniq? I'd like to see uniq (or something) that strips : out the duplicates in an array. It would be cool if it were : built into perl. : : Meanwhile, has anyone written an efficient routine to do this? : Linear time would be nice. I'd like one that doesn't require the : array to be sorted. I could write one myself, but I thought : I'd avoid duplicating code.. sub uniq { local(%seen); grep(!$seen{$_}++, @_); } @out = &uniq(@in); It's more-or-less linear, apart from the extra time to bifurcate %seen occasionally as it's growing. I don't know whether you want to call it efficient--it's going to depend on how quickly your machine can calculate the hash function. It may be faster to sort and compare adjacent values, depending... Larry