I have a List, for example (assume it's always ordered like this one):
myList = {6, 6, 8, 10, 14}
I would like to get a result like this:
desiredResult = {{6,2}}
in other words "6 appears more than once. Nothing else appears more than once"
This is where I'm at now - clearly only partway there.
partialResult =
Table[Cases[myList, myList[[i]] ..], {i, 1, Length[myList]}] // DeleteDuplicates
which gives me this result:
partialResult = {{6, 6}, {8}, {10}, {14}}
and then
finalResult =
Table[
{partialResult[[i, 1]], Length[partialResult[[i]]]},
{i, 1, Length[partialResult]}]
gives me almost desiredResult
, but I'm not satisfied. My solution seems clunky. I'd love to see an elegant solution to this - there has to be a cleaner, more pleasing way to do this. It's using functions, but the repeated use of Table & Length
, and the egregious DeleteDuplicates
just feels like disguised procedural code.
I'd really appreciate seeing better ways to get partialResult
and finalResult
(I could learn a lot from those two items), but I expect that there's a better way to do it than the multi-step.
Select[Tally[myList], #[[2]] > 1 &]
$\endgroup$