Home > Blockchain >  Find the repeating numbers in order?
Find the repeating numbers in order?

Time:12-10

I have tried different repeating functions which tell me the frequency of the numbers, but I want to know the order of the repeating numbers. For example I have an array which has numbers

a=[ 1,1,1,1,1,2,2,2,2,2,1,1,1,1,4,4,4,5,5,5,7,7,2,2,2,2]

I want my function to give me the answer, i.e. 1,2,1,4,5,7,2.

CodePudding user response:

You can use the diff, you're interested in the first element of a and then every subsequent index where the difference between elements is non-zero

>> a = [ 1,1,1,1,1,2,2,2,2,2,1,1,1,1,4,4,4,5,5,5,7,7,2,2,2,2];

>> b = a( [true, diff(a)~=0] );
b =
     1     2     1     4     5     7     2

  • Related