#include <iostream>
#include <vector>
#include <algorithm>
// Helper function to remove duplicates
void rmdupHelper(std::vector<int> &arr, int n) {
// Base case: If we've reached the end of the vector, return
if (n >= arr.size() - 1) {
return;
}
// If the current element is the same as the next element, remove the duplicate
if (arr[n] == arr[n + 1]) {
arr.erase(arr.begin() + n + 1);
// Don't increment n since we need to check the new element at the current position
rmdupHelper(arr, n);
} else {
// Move to the next element
rmdupHelper(arr, n + 1);
}
}
// Main function to remove duplicates
std::vector<int> rmdup(std::vector<int> &arr) {
rmdupHelper(arr, 0);
return arr;
}
/*for(int i=0; i<arr.size();i++){
for(int j=i+1; j<arr.size(); j++){
if(arr[i]==arr[j]){
for(int k=i;k<arr.size()-1;k++){
arr[k] = arr[k+1];
}
}
}
}*/
void bubbleSortHelper(std::vector<int> &arr, int n) {
// Base case
if (n == 1)
return;
// One pass of bubble sort. After this pass, the largest element is moved (bubbled) to the end.
for (int j = 0; j < n - 1; j++) {
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
}
}
// Largest element is fixed, recur for remaining array
bubbleSortHelper(arr, n - 1);
}
void sort(std::vector<int> &arr) {
bubbleSortHelper(arr, arr.size());
}
/*for(int i=0; i<arr.size(); i++){
for(int j=0; j<arr.size()-1; j++){
if(arr[j] > arr[j+1]){
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}*/
int main() {
std::vector<int> arr = {100,42,0,8,0,5,1,82};
std::vector<int> sub = {82,5,0,1,42,8};
arr.insert(arr.end(), sub.begin(), sub.end());
// Sort the array first
//std::sort(arr.begin(), arr.end());
sort(arr);
arr = rmdup(arr);
for(int i=0;i<arr.size();i++){
std::cout<<arr[i]<<' ';
}
std::cout<<std::endl;
for (int i = 0; i < sub.size(); i++) {
for (int j = 0; j < arr.size(); j++) {
if (sub[i] == arr[j]) {
std::cout << j << ' ';
break; // Stop after finding the first match
}
}
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: