Let's start with an interview question.
A majority element is an element in array constitutes more than 50% of elements. For example, consider the numbers:
Here, the majority element is 99.
Challenge: Find the majority element in linear time using memory.
Use Moore's voting algorithm.
C arrays and strings are useful to know for legacy codebases, but yo u should always use array<T>
and vector<T>
instead for projects.
for
loopsIn C++11, there are for-in
loops. They are used like so:
for (auto x : array) { // use x }
If you want to change the values in array
, use:
for (auto &x : array) { // change array values }
Objects that contain multiple data items, such as int
s, double
s, or objects. They allow for control and protection over editing of objects, and can copy/edit/sort/order many objects at once.
They're also very combinable - you can have a vector of stacks.