J
J
Jestome2018-10-12 19:12:47
Python
Jestome, 2018-10-12 19:12:47

How to select the minimum value from the number of identical array elements?

Something like the task is not difficult, but I have been sitting for the 2nd day already ...
The essence is simple and is described in detail in the title)) :
You need to find the number of repetitions of each of the elements in an integer array and display the minimum number. That is, when:

a = [6, 2, 4, 4, 2, 2, 6, 4]
# Программа должна вывести 6

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
eRKa, 2018-10-12
@kttotto

I'm not in the python part, but here, catch

a = [1, 2, 4, 4, 2, 2, 1, 4]
length = len(a)

i = 0
max = a[i]

while (i < length):
  if(a[i] > max):
    max = a[i]
  i += 1

counter = 0
i = 0
result = length

while (i < length):
  if(max == a[i]):
    counter = counter + 1
    if(i == (length - 1) or a[i+1] != max):
      if(counter < result):
        result = counter
        counter = 0
  i += 1

print(result)
input()

A
Alexander Skusnov, 2018-10-13
@AlexSku

If I understand correctly, you need grouping. Here is my Haskell solution (I'm a beginner, so there might be better options). At least (comments) the algorithm is clear.

import Data.List

a = [1, 2, 4, 4, 2, 2, 1, 4]

-- группируем
b = group a                                    -- 

-- фильтруем список b, содержащие максимум списка a
c = let f (x:_) = x == maximum a in filter f b -- 

-- минимальная длина группы
minimum . map length $ c                       -- 1

Here is the chain in one line:
minimum . map length . filter ((== maximum a) . head ) . group $ a

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question