-
Notifications
You must be signed in to change notification settings - Fork 0
/
majority_element.hpp
79 lines (63 loc) · 1.8 KB
/
majority_element.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) Omar Boukli-Hacene. All rights reserved.
// Distributed under an MIT-style license that can be
// found in the LICENSE file.
// SPDX-License-Identifier: MIT
/// Problem source:
/// https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
#ifndef FORFUN_MAJORITY_ELEMENT_HPP_
#define FORFUN_MAJORITY_ELEMENT_HPP_
#include <iterator>
namespace forfun::majority_element {
template <typename Elements>
requires std::forward_iterator<typename Elements::iterator>
[[nodiscard]] constexpr auto majority_element(Elements const& elements) noexcept
-> Elements::const_iterator
{
using ElementType = Elements::value_type;
using SizeType = Elements::size_type;
using Itr = Elements::const_iterator;
SizeType const size{elements.size()};
Itr const cend{elements.cend()};
if (size < SizeType{3U})
{
return cend;
}
SizeType threshold{(size / SizeType{2U}) + SizeType{1U}};
SizeType count{0U};
ElementType majority_elm{} /*[[indeterminate]]*/;
Itr majority_itr{} /*[[indeterminate]]*/;
for (Itr itr{elements.cbegin()}; itr != cend; ++itr)
{
if (count == SizeType{0U})
{
majority_elm = *itr;
majority_itr = itr;
}
if (*itr == majority_elm)
{
++count;
if (count == threshold)
{
return majority_itr;
}
}
else
{
--count;
}
}
for (auto itr{elements.cbegin()}; itr != cend; ++itr)
{
if (*itr == majority_elm)
{
--threshold;
}
if (threshold == SizeType{0U})
{
return majority_itr;
}
}
return cend;
}
} // namespace forfun::majority_element
#endif // FORFUN_MAJORITY_ELEMENT_HPP_