Finding non-duplicate items in a list. Given a list of numbers, print all non-duplicate items.
nonDup<-function(x)
{
y<-NULL;
while(length(x)>=1)
{
tmp<-(1:length(x))[x==x[1]];
if(length(tmp)==1) y<-c(y,x[1]);
x<-x[-tmp];
}
return(y);
}
To test the above implementation, run
>x<-c(1,2,3,2,5,3,6);
>nonDup(x)
>x<-c(1,2,3);
>nonDup(x)
nonDup<-function(x)
{
if(length(x)==1) return(x);
x<-sort(x);
y<-NULL;
cnt<-1;prevx<-x[1];
for(i in 2:length(x))
{
if(x[i]!= prevx)
{ if(cnt==1)
{ y<-c(y,prevx); }
else cnt<-1;
}
else { cnt<-cnt+1;}
prevx<-x[i];
}
if(cnt==1) y<-c(y,x[i]);
return(y);
}
To test the above implementation, run
>x<-c(1,2,3,2,5,3,6);
>nonDup(x)
>x<-c(1,2,3);
>nonDup(x)