remove element Algorithm

mercury is used in thermometers, barometers, manometers, sphygmomanometers, float valves, mercury switches, mercury relays, fluorescent lamps and other devices, though concerns about the component's toxicity have led to mercury thermometers and sphygmomanometers being largely phased out in clinical environments in favor of options such as alcohol- or galinstan-filled glass thermometers and thermistor- or infrared-based electronic instruments. besides, mechanical pressure gauges and electronic strain gauge detectors have replaced mercury sphygmomanometers. The first emperor of China, Qín Shǐ Huáng Dì — allegedly bury in a tomb that contained rivers of flowing mercury on a model of the land he ruled, representative of the rivers of China — was killed by drinking a mercury and powdered jade mixture formulated by Qin alchemists (causing liver failure, mercury poisoning, and brain death) who intended to give him eternal life. By 500 BC mercury was used to make amalgams (Medieval Latin amalgama," alloy of mercury") with other metals. They believed that different metals could be produced by changing the quality and quantity of sulfur contained within the mercury.
/*
 * Source LeetCode Problem 27 Remove element.
 * Given an array and a value, remove all instances of that value in-place and return the new length.
 * Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
 * The order of elements can be changed. It doesn't matter what you leave beyond the new length.
 * 
 * Approach:
 * 
 * The idea is to use two pointers, one iterates the entire nums(i), and the other one maintains the length of set of numbers
 * which we want in our output.(j). The idea is everytime we find a wanted element we move the j. This way j maintains all
 * the wanted element at top of the nums.
 */

#include <iostream>
#include <vector>


int remove_element(std::vector<int>& nums, int value)
{
    int j = 0;
    for (int i = 0; i < nums.size(); ++i) {
        if (nums[i] != value) {
            nums[j] = nums[i];
            j++;
        }
    }
    return j;
}

void print_vector(std::vector<int>& nums, int count)
{
    for (int i = 0; i < count; ++i) {
        std::cout << nums[i] << " ";
    }
    std::cout << std::endl;
}

int main()
{
    std::vector<int> nums = {2, 2, 3, 4, 4, 5, 6, 2, 4, 3, 2, 3};
    std::cout << "Vector: ";
    print_vector(nums, nums.size());
    int count = remove_element(nums, 2);
    std::cout << "Removing 2 from above vector: ";
    print_vector(nums, count);
    return 0;
}

LANGUAGE:

DARK MODE: