2

I know how can I find the number of duplicates from a numpy array. However, I need to find the number of duplicates only at the end of the numpy array. Please see my example below:

Example input is as follows:

1995
1996
1996
1997
1998
1999
1999
1999

Desired output:

3

Thanks in advance!

3 Answers 3

3

Here's one way with np.minimum.accumulate -

np.minimum.accumulate(a[::-1]==a[-1]).sum()

Sample run -

In [64]: a
Out[64]: array([2, 1, 9, 0, 0, 0, 2, 1, 0, 0, 0, 0, 2, 1, 0, 9, 9, 9, 9, 9])

In [73]: np.minimum.accumulate(a[::-1]==a[-1]).sum()
Out[73]: 5

Another with argmin -

In [88]: (a[::-1]==a[-1]).argmin()
Out[88]: 5

For a corner case, if all elements are same, we might need one extra step to check for all matches on a[::-1]==a[-1] and return len(a) in that case. Or if the count is 0, which can't be the output, we will output len(a) instead.

Sign up to request clarification or add additional context in comments.

4 Comments

Hey, why not just sum it like np.sum( data == data[-1] ) , any specific advantage of np.minimum.accumulate ?
@PavanKumarPolavarapu Because we want to skip the intermediate elements that matches the last element if I understood OP's intent of only at the end of the numpy array correctly.
Umm, makes sense :)
@PavanKumarPolavarapu many thanks for your attention. only at the end of the numpy array was key for this question.
0
mylist = [1995,
1996,
1996,
1997,
1998,
1999,
1999,
1999]
s = 0
x = mylist[-1]
for i in mylist:
   if i == x: s+= 1
print(s)

1 Comment

Thanks. Your code will return the correct results however there should be break when the loop encounter a non-duplicate.
0

My solution is as follows, however, I am sure that there is a more elegant solution in a line!

def get_numlastdups(my_nparray):
    numlastdups=0
    for i in range(2,len(my_nparray)):
        if my_nparray[-1]==my_nparray[-i]: numlastdups+=1
        else: break 
    return numlastdups

2 Comments

This will not work - since you don't break the loop on hitting a non-duplicate, all values equal to the last one (not just those in a streak) will be counted.
@Blotosmetek Actually it will work since i only consider the last element in the if section, but you've a point about the break. I added a break.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.