0

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?

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

2 Answers2

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')