5
$\begingroup$

I have the list

t1 = {{-1, 0}, {-2, 0}, {-3, 0}, {0, 0}, {-2, 0}, {1, 1}}

How do I find the position where an element repeats? In this case it would be element {-2, 0} at position 5, because {-2, 0} first came up at postion 2. So the answer would be 5.

I made it to comparisons with the first element but don't know how to procede.

$\endgroup$
3
$\begingroup$
firstDup = Min @* Map[Rest] @* Values @* PositionIndex;;

firstDup @ {{-1, 0}, {-2, 0}, {-3, 0}, {0, 0}, {-2, 0}, {1, 1}}
5
firstDup @ {{-1, 0}, {-2, 0}, {-3, 0}, {0, 0}, {-2, 0}, {1, 1}, {-2, 0}, {-2, 0}}
5
firstDup @ {{-1, 0}, {-2, 0}, {-3, 0}, {-1, 0}, {0, 0}, {-2, 0}, {1, 1}}
4
| improve this answer | |
$\endgroup$
2
$\begingroup$

If you just want the position of the first duplicate element you could use something like this,

firstDuplicatePosition[list_]:=With[{ds = CreateDataStructure @ "HashSet"},
    Catch[
        MapIndexed[If[!ds["Insert",#1],Throw[#2]]&, list];
        {}
    ]
];
firstDuplicatePosition @ {{-1, 0}, {-2, 0}, {-3, 0}, {0, 0}, {-2, 0}, {1, 1}}
(* {5} *)
| improve this answer | |
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.