find.most.repeated<-function(x) {
 # this will only work if all components are matrices, arrays or data frames
 ncomponents<-length(x)
 if(ncomponents == 1) stop("Can't compare anything, only one component")
 paircomps<-combn(1:ncomponents,2)
 ncomps<-dim(paircomps)[2]
 compsums<-rep(NA,ncomps)
 for(comp in 1:ncomps) {
  xdim1<-dim(x[[paircomps[1,comp]]])
  xdim2<-dim(x[[paircomps[2,comp]]])
  if(sum(xdim1 != xdim2) == 0)
   compsums[comp]<-sum(x[[paircomps[1,comp]]]  != x[[paircomps[2,comp]]])
  else {
   # get the conformable portion of the components
   mindim<-c(min(c(xdim1[1],xdim2[1])),min(c(xdim1[2],xdim2[2])))
   # count all nonconformable elements as mismatches and add the conformable mismatches
   compsums[comp]<-(xdim1[1]-mindim[1])*mindim[2] + (xdim1[2]-mindim[2])*mindim[1] +
    sum(x[[paircomps[1,comp]]][1:mindim[1],1:mindim[2]] !=
    x[[paircomps[2,comp]]][1:mindim[1],1:mindim[2]])
  }
 }
 # collect the indices of the comparisons with the least mismatches
 bestmatches<-which(compsums == min(compsums))
 return(list(bestmatches=paircomps[,bestmatches],components=x[bestmatches]))
}
