In an array [1,2,2,3]
the mode is 2 and it occurs two times. How to get this as output for a particular array?
Asked
Active
Viewed 426 times
0

James Z
- 12,209
- 10
- 24
- 44

Aarsha Leena
- 1
- 1
-
What do you mean by mode? – deets Aug 29 '21 at 12:27
-
2Does this answer your question? [Finding the mode of a list](https://stackoverflow.com/questions/10797819/finding-the-mode-of-a-list) – HPringles Aug 29 '21 at 12:27
2 Answers
1
You could use collections
module form python and call in a counter
for maximum occurrence of a number.
from collections import Counter
sample = [10, 10, 30, 10, 50, 30, 60]
print("Mode of List A is % s" % (Counter(sample).most_common(1)[0][0]))
If you are not relying much on the concept of how it is built, the easier way is to use the statistics
module and get the mode.
Example:
from statistics import mode
sample=[1,2,2,2,3,4]
print(mode(sample))

Roxy
- 1,015
- 7
- 20
0
There are so many ways... But I guess the best one is:
Defining a function like:
def DuplicatesCheck(input_array):
for el in input_array:
if input_array.count(el) > 1:
return True
return False
Then call it this way:
your_array = [1,2,2,3]
result = DuplicatesCheck(your_array)
if result:
print('Positive')
else:
print('Negative')