I would approach this as follows:
x <- list(c("10", "719", "99"),
c("10", "624", "85", "888", "624"),
c("1", "894", "110", "344", "634"))
first_elems <- sapply(x, "[", 1) # get 1st elem of each vector
(first_elems <- as.factor(first_elems)) # factorize (i.a. find all unique elems)
## [1] 10 10 1
## Levels: 1 10
(group <- split(x, first_elems)) # split by 1st elem (divide into groups)
## $`1`
## $`1`[[1]]
## [1] "1" "894" "110" "344" "634"
##
##
## $`10`
## $`10`[[1]]
## [1] "10" "719" "99"
##
## $`10`[[2]]
## [1] "10" "624" "85" "888" "624"
##
(result <- lapply(group, unlist)) # combine vectors in each group (list of vectors -> an atomic vector)
## $`1`
## [1] "1" "894" "110" "344" "634"
##
## $`10`
## [1] "10" "719" "99" "10" "624" "85" "888" "624"
EDIT . For non-duplicated keys, use:
(result <- lapply(group, function(x) {
c(x[[1]][1], unlist(lapply(x, "[", -1)))
}))
## $`1`
## [1] "1" "894" "110" "344" "634"
##
## $`10`
## [1] "10" "719" "99" "624" "85" "888" "624"
No extra memory required. With the exception of the resulting list, we need to save the results as.factor(number of classes + number of elements in x). splitrequires a little extra mem - vectors in xare not deeply copied.
In terms of performance, for a fairly large list:
set.seed(1L)
n <- 100000
x <- vector('list', n)
for (i in 1:n)
x[[i]] <- as.character(sample(1:1000, ceiling(runif(1, 1, 1000)), replace=TRUE))
object.size(x)
Linux- :
system.time(local({
first_elems <- as.factor(sapply(x, "[", 1))
group <- split(x, first_elems)
result <- lapply(group, function(x) {
c(x[[1]][1], unlist(lapply(x, "[", -1)))
})
}))
, .