- uniq -c
- array_count_values
1 Bash[ | ]
Bash
Copy
ARR=(1 "hello" 1 "world" "hello")
for VALUE in "${ARR[@]}"; do
echo $VALUE
done | sort | uniq -c
Loading
2 POSIX Shell[ | ]
Bash
Copy
ARR="1 hello 1 world hello"
for VALUE in $ARR
do
echo $VALUE
done | sort | uniq -c
Loading
3 C[ | ]

4 PHP[ | ]

PHP
Copy
$arr = [1, 'hello', 1, 'world', 'hello'];
print_r(array_count_values($arr));
Loading
5 Python[ | ]
Python
Copy
import collections
lst = [1, 'hello', 1, 'world', 'hello']
print( collections.Counter(lst) )
Loading
6 Perl[ | ]
Perl
Copy
my %cnt;
my @lst = (1, 'hello', 1, 'world', 'hello');
$cnt{$_}++ for @lst;
print %cnt;
Loading
7 R[ | ]

R
Copy
v <- c(1, 'hello', 1, 'world', 'hello')
table(v)
Loading
8 SQL[ | ]
8.1 MySQL[ | ]
Console
Copy
mysql> SELECT * FROM array;
+-------+
| value |
+-------+
| 1 |
| hello |
| 1 |
| world |
| hello |
+-------+
5 rows in set (0.00 sec)
Console
Copy
mysql> SELECT value, COUNT(*) FROM array GROUP BY value;
+-------+----------+
| value | COUNT(*) |
+-------+----------+
| 1 | 2 |
| hello | 2 |
| world | 1 |
+-------+----------+
3 rows in set (0.00 sec)