Questions tagged [counting]

Counting is the action of finding the number of elements of a finite set of objects.

Counting is the action of finding the number of elements of a finite set of objects. The traditional way of counting consists of continually increasing a (mental or spoken) counter by a unit for every element of the set, in some order, while marking (or displacing) those elements to avoid visiting the same element more than once, until no unmarked elements are left; if the counter was set to one after the first object, the value after visiting the final object gives the desired number of elements. The related term enumeration refers to uniquely identifying the elements of a finite (combinatorial) set or infinite set by assigning a number to each element.

Counting using tally marks at Hanakapiai Beach Counting sometimes involves numbers other than one; for example, when counting money, counting out change, "counting by twos" (2, 4, 6, 8, 10, 12, ...), or "counting by fives" (5, 10, 15, 20, 25, ...). There is archeological evidence suggesting that humans have been counting for at least 50,000 years. Counting was primarily used by ancient cultures to keep track of social and economic data such as number of group members, prey animals, property, or debts (i.e., accountancy). The development of counting led to the development of mathematical notation, numeral systems, and writing.

1837 questions
31
votes
5 answers

How do I count unique visitors to my site?

I am making a visitor counting system for user posts to show the most viewed posts on homepage. I have a visitor counting system now but all it registers a view at every page refresh. I cannot use Google Analytics. What I need is a visitor counter…
Nirav
  • 560
  • 1
  • 7
  • 17
30
votes
4 answers

Function to count number of digits in string

I was looking for a quick PHP function that, given a string, would count the number of numerical characters (i.e. digits) in that string. I couldn't find one, is there a function to do this?
Ashley Strout
  • 6,107
  • 5
  • 25
  • 45
28
votes
3 answers

Counting the amount of occurrences in a list of tuples

I am fairly new to python, but I haven't been able to find a solution to my problem anywhere. I want to count the occurrences of a string inside a list of tuples. Here is the list of tuples: list1 = [ ('12392', 'some string', 'some other…
mackwerk
  • 1,689
  • 4
  • 21
  • 44
27
votes
8 answers

Find the number of files in a directory

Is there any method in Linux to calculate the number of files in a directory (that is, immediate children) in O(1) (independently of the number of files) without having to list the directory first? If not O(1), is there a reasonably efficient…
yassin
  • 6,529
  • 7
  • 34
  • 39
23
votes
2 answers

What is the Julia function to count combinations (n choose k)?

I'm looking for the (hopefully built-in) function in Julia that calculates the number of combinations I could obviously implement my own using factorials, but I'm almost certain somebody has already worried about this.
rodrigolece
  • 1,039
  • 2
  • 13
  • 19
21
votes
10 answers

How do I count the characters, words, and lines in a file, using Perl?

What is a good/best way to count the number of characters, words, and lines of a text file using Perl (without using wc)?
NoahD
  • 8,092
  • 4
  • 27
  • 28
16
votes
4 answers

How does this code work to count number of 1-bits?

I found the following code to count the number of 1-bits in binary representation for given integer. Can anyone explain how it works? And how are the bit masks chosen for such task? Thanks. int count_one(int x) { x = (x & (0x55555555)) + ((x >>…
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
14
votes
6 answers

How to count the consecutive duplicate values in an array?

I have an array like this: $arr = array(1, 1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3); I found the function array_count_values(), but it will group all of the same values and count the occurrences without respecting breaks in the consecutive…
Jeg Bagus
  • 4,895
  • 9
  • 43
  • 54
14
votes
2 answers

No. of distinct subsequences of length 3 in an array of length n

How to calculate the number of distinct sub sequences of length 3 (or in general of length k < n) in an array of length n? Note: Two sub sequences are considered different if the order of elements in them are different. Ex: Suppose the array A = [1,…
dodobhoot
  • 493
  • 6
  • 15
13
votes
5 answers

Group rows in a 2d array and count number of rows in each respective group

I have an array of 200 items. I would like to output the array but group the items with a common value. Similar to SQL's GROUP BY method. This should be relatively easy to do but I also need a count for the group items. Does anyone have an efficient…
GivP
  • 2,634
  • 6
  • 32
  • 34
13
votes
5 answers

Create group number for contiguous runs of equal values

Is there is a faster way to make a counter index than using a loop? For each contiguous run of equal values, the index should be the same. I find the looping very slow especially when the data is so big. For illustration, here is the input and…
Rens
  • 133
  • 5
13
votes
3 answers

How to count number of digit included zero in php

I am working to count number of digit in PHP. I just want to count number of digit value for database.In first number have zero means , it won't take as a number for counting. for example: 12 ==number of count value is 2 122 ==number of count…
user2388680
12
votes
5 answers

Counting the number of occurrences of a string within a string

What's the best way of counting all the occurrences of a substring inside a string? Example: counting the occurrences of Foo inside FooBarFooBarFoo
MBZ
  • 26,084
  • 47
  • 114
  • 191
11
votes
4 answers

Pyspark dataframe: Count elements in array or list

Let us assume dataframe df as: df.show() Output: +------+----------------+ |letter| list_of_numbers| +------+----------------+ | A| [3, 1, 2, 3]| | B| [1, 2, 1, 1]| +------+----------------+ What I want to do is to count number of a…
Ala Tarighati
  • 3,507
  • 5
  • 17
  • 34
11
votes
6 answers

Count the number of Ks between 0 and N

Problem: I have seen questions like: count the number of 0s between 0 and N? count the number of 1s between 0 and N? count the number of 2s between 0 and N? ... ... These kinds of questions are very similar of asking to find the total number that…
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174