Re: [Jprogramming] Find nth duplicate in vector

2022-01-28 Thread Raul Miller
t; |3|9 12 | > +-+-+ > |0|11 15| > +-+-+ > |5|14 16| > +-+-+ > > > R.E. Boss > > > -Original Message- > From: Programming On Behalf Of > Raul Miller > Sent: donderdag 27 januari 2022 19:1

Re: [Jprogramming] Find nth duplicate in vector

2022-01-28 Thread R.E. Boss
:11 To: Programming forum Subject: Re: [Jprogramming] Find nth duplicate in vector Well, ... the implementation posted by xash is very nice, but in http://jsoftware.com/pipermail/programming/2022-January/059790.html Pawel Jakubas specified that the value appears first, and the index appears second

Re: [Jprogramming] Find nth duplicate in vector

2022-01-27 Thread Raul Miller
Well, ... the implementation posted by xash is very nice, but in http://jsoftware.com/pipermail/programming/2022-January/059790.html Pawel Jakubas specified that the value appears first, and the index appears second, and that the indices start with 1 for the first value. Also, somewhere along the

Re: [Jprogramming] Find nth duplicate in vector

2022-01-27 Thread Hauke Rehr
I wonder why there’s still so much traffic on this thread (okay, I’m to blame for quite some of it) I thought xash published the best correct solution. Am I wrong? Am 27.01.22 um 18:42 schrieb 'Mike Day' via Programming: Pawel Jakubas has moved on to something else in his correspondence,

Re: [Jprogramming] Find nth duplicate in vector

2022-01-27 Thread 'Mike Day' via Programming
Pawel Jakubas has moved on to something else in his correspondence, presumably related to this topic. Anyway, I think we need his adjudication, as I don't think Raul Miller's f is doing what PJ asked for. Hauke Rehr's proposal is hrf =: {{ (,~ {) {. I. (>: x) e."1 +/\ =/~ y }} My latest

Re: [Jprogramming] Find nth duplicate in vector

2022-01-27 Thread Raul Miller
Here's how I would modify my implementation to produce an empty result when a non-duplicate is referenced. d=: 1 _1 2 3 4 2 5 6 3 8 10 3 2 f=: {{y ({~,]) I. wrote: > > Perhaps I should point out that my verb, h (see below), uses a lot of space > for a large list, especially if there are

Re: [Jprogramming] Find nth duplicate in vector

2022-01-27 Thread 'Mike Day' via Programming
Perhaps I should point out that my verb, h (see below), uses a lot of space for a large list, especially if there are few repeats, as it’s using an outer product. I later produced an even more verbose effort which runs in less space on a 10^6 long vector, but takes 3-4x the space & time

Re: [Jprogramming] Find nth duplicate in vector

2022-01-26 Thread 'Mike Day' via Programming
Unfortunately, Pawel wants 2 f d to be 3 11. However, I find that 3 f d IS 3 11. Other results are a bit strange, too: 4 f d 2 12 8 f d |index error: f | y({~,])x i.~+/\(i.@#~:i.~)y I wasn’t going to post my effort, but it might interest Pawel. This version works on the

Re: [Jprogramming] Find nth duplicate in vector

2022-01-26 Thread xash
-.@~: gets a mask of duplicates, I. then their indices. With them you can get a list of (index, value) pairs: ((],.{~) I.@:-.@~:) a 5 2 8 3 11 3 12 2 With 0 ({ (],.{~) I.@:-.@~:)) a you can 0-index into this list. If you are only interested into the first duplicate of a value, you could filter

Re: [Jprogramming] Find nth duplicate in vector

2022-01-26 Thread Hauke Rehr
As Henry pointed out, do take a look at [1] for how to approach this in J If you want to understand my solution, just try it step by step. I repeat it here, with a bit of litter removed: f =: {{ (,~ {) {. I. (>: x) e."1 +/\ =/~ y }} NB. where does each number occur? =/~ d 1 0 0 0 0 0 0 0

Re: [Jprogramming] Find nth duplicate in vector

2022-01-26 Thread Raul Miller
Here's a variation that works: d=: 1 _1 2 3 4 2 5 6 3 8 10 3 2 f=: {{y ({~,]) x i.~ +/\(i.@#~:i.~)y }} 1 f d 2 5 2 f d 3 8 The phrase (i.@# ~: i.~) finds the locations of duplicates (i.@#~:i.~) 1 _1 2 3 4 2 5 6 3 8 10 3 2 0 0 0 0 0 1 0 0 1 0 0 1 1 And, +/\ computes a running sum

[Jprogramming] Find nth duplicate in vector

2022-01-26 Thread Pawel Jakubas
It should be of course 1 f d 2 5 Would be great if you could decompose your solution and the idea behind the solution. Many thanks. Cheers, Pawel -- For information about J forums see http://www.jsoftware.com/forums.htm

Re: [Jprogramming] Find nth duplicate in vector

2022-01-26 Thread Henry Rich
Use the ideas in https://code.jsoftware.com/wiki/Essays/Progressive_Index-Of . Henry Rich On 1/26/2022 1:37 PM, Pawel Jakubas wrote: 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

Re: [Jprogramming] Find nth duplicate in vector

2022-01-26 Thread Hauke Rehr
Maybe this is what you want (but it’s not elegant) f =: {{ (,~ {) {. I. ( (>: x)"1) +/\ =/~ y }} 1 f d 2 5 2 f d 3 11 just a quick and dirty working solution (if 2 5 is the correct answer to 1 f d) Am 26.01.22 um 19:37 schrieb Pawel Jakubas: Dear J enthusiasts, I wonder how to

Re: [Jprogramming] Find nth duplicate in vector

2022-01-26 Thread Hauke Rehr
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

[Jprogramming] Find nth duplicate in vector

2022-01-26 Thread 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