H
H
Helcurt2020-10-13 09:10:52
C++ / C#
Helcurt, 2020-10-13 09:10:52

How to determine the number of duplicate elements in an array?

Find the number of duplicate elements in an array. The only thing that comes to my mind is a search through the condition ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Armenian Radio, 2020-10-13
@Radia

Alas, this is the only way it works (for the general case of strings). If you can use stl, use std::map like this

std::vector<std::map<char, size_t>> maps(strings.size());

size_t nmax = 0;
size_t fmax = 0;

for(size_t i=0;i<strings.size();++i)
{
    for(const auto& j:strings[i])
    {
        const size_t current = ++maps[i][j];
        if(current>fmax)
        {
            fmax=current;
            nmax=i;
        }
    }
}

//теперь в nmax у нас номер строки с максимально частым повторением

A
Anton Zhilin, 2020-10-13
@Anton3

std::vector<int> row = ...;
std::unordered_map<int, size_t> counts;
for (int x : row) ++counts[x];
size_t n_duplicates = row.size();
for (auto p : counts) {
  if (p.second == 1) --n_duplicates;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question