listcountparipari-gp

how to get the count of a number in a List PARI/GP


is there a method to get the count of a given number in a List? I have a List for which I want to verify, that no number is more than x-times in it.


Solution

  • For input being list of integers, you can use the matreduce. It returns the 2-column matrix. Its first column will contain the unique elements of your input list. The second column will contain the counts. See the example below.

    > x = [1,2,1,1,2,2,2,3,1,2,1,3];
    > M = matreduce(x)
    [1 5]
    [2 5]
    [3 2]
    

    So, just review the second column to ensure that no its element is more than N-times.

    > N = 4;
    > #select(elt -> elt > N, M[,2])  % count the numbers greater than N.
    2