5

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.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
user57467
  • 2,708
  • 6
  • 12
  • 4
    How to efficiently find position of duplicates (https://mathematica.stackexchange.com/questions/21341/how-to-efficiently-find-positions-of-duplicates) – LouisB Apr 07 '20 at 18:36

2 Answers2

3
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
kglr
  • 394,356
  • 18
  • 477
  • 896
3

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} *)
Jason B.
  • 68,381
  • 3
  • 139
  • 286