Here’s the (probably naive) way I’ve done it:
count =: 4 : '# (#~ =&x) y'"1 0 1
In other words, if I say 4 count 3 4 4 3 4 7 9 the result is 3, because 4 occurs 3 three times in the given list.
This works perfectly, but I wonder whether J offers some more concise way to phrase this.
When I do this, knowing that I’m only ever going to have a list and not a matrix, I use:
Or for multiple searches in the list:
Your method copies over only the elements that are equal to x, then counts the result. Summing the ones for equality is one less operation.