# [Rd] unique.default problem (PR#12551)

ripley at stats.ox.ac.uk ripley at stats.ox.ac.uk
Sun Aug 17 11:40:13 CEST 2008

```The problem here is that there are more incomparables than x. But in any
case the answer was incorrect:

> unique(rep("a", 3), "a")
[1] "a"

I've fixed both issues via a different algorithm for 2.7.2.

On Sat, 16 Aug 2008, prokaj at cs.elte.hu wrote:

> Full_Name: Vilmos Prokaj
> Version: R 2.7.1
> OS: windows
> Submission from: (NULL) (213.181.195.84)
>
>
> Dear developers,
>
> The following line of code (produced by a mistake) caused an infinite loop
>
> unique("a",c("a","b"))
>
> or also
>
> unique(1,1:2)
>
> I made   a little investigation, and it seems to be that the following function
> from unique.c is looping infinitely
>
>
> static int isDuplicated(SEXP x, int indx, HashData *d)
> {
>    int i, *h;
>
>    h = INTEGER(d->HashTable);
>    i = d->hash(x, indx, d);
>    while (h[i] != NIL) {
> 	if (d->equal(x, h[i], x, indx))
> 	    return h[i] >= 0 ? 1 : 0;
> 		i = (i + 1) % d->M;
> 	    }
> 	    h[i] = indx;
>    return 0;
> }
> In this case h contains only one negative value, which causes d->equal(=requal)
> to return 0.
>
> static int requal(SEXP x, int i, SEXP y, int j)
> {
>    if (i < 0 || j < 0) return 0;
>    if (!ISNAN(REAL(x)[i]) && !ISNAN(REAL(y)[j]))
> 	return (REAL(x)[i] == REAL(y)[j]);
>    else if (R_IsNA(REAL(x)[i]) && R_IsNA(REAL(y)[j])) return 1;
>    else if (R_IsNaN(REAL(x)[i]) && R_IsNaN(REAL(y)[j])) return 1;
>    else return 0;
> }
>
> I do not claim that the situation above is frequent or even meaningful, however
> it should not cause a crash of R.
>
> Sincerely yours
> Vilmos Prokaj
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

```