[Jprogramming] Find nth duplicate in vector

Hauke Rehr hauke.rehr at uni-jena.de
Wed Jan 26 18:59:25 UTC 2022


I don’t understand why it’s index 2 (first occurrence of 2)
but 11 (third occurrence of 3)

Am 26.01.22 um 19:37 schrieb Pawel Jakubas:
> Dear J enthusiasts,
> 
> I wonder how to elegantly implement dyad f that takes as x=1,2,3... and y
> vector and returns nth duplicate value with index of that occurence:
> 
>     d=: 1 _1 2 3 4 2 5 6 3 8 10 3 2
>     NB. the earliest first duplicate is for 2 and index 2
>    1 f d
> 2 2
>    NB. the earliest second duplicate is for 3 for index 11
>    2 f d
> 3 11
> 
> In Haskell for nth occurence I would just use accumulator as
> (Map with key being element and value the number of occurence of this
> element,
> current index count)
> and left fold that with starting accumulator (Map.empty, 0).
> 
> Kindest regards,
> Pawel
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
> 

-- 
----------------------
mail written using NEO
neo-layout.org



More information about the Programming mailing list