the first step to generate a random array with the size as you want
The size of the array is
18
1 2 3 7 5 1 1 3 7 4 7 3 2 8 1 1 7 6
The corresponding codes:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cstring>
using namespace std;
void generate_array(vector<int>& arr, int n);
int main()
{
cout << "The size of the array is " ;
int n;
cin >> n;
srand((unsigned)time(NULL));
vector<int> arr;
generate_array(arr, n);
for_each(arr.begin(), arr.end(), [](int x){ cout << setw(2) << x ;});
return 0;
}
void generate_array(vector<int>& arr, int n)
{
for (int i = 0; i < n; i++){
arr.push_back(rand() % 10);
}
}
The second is to sort out the elements in array in increasing order:
the results:
The size of the array is 16
4 1 3 8 9 6 4 8 6 4 1 5 9 8 8 5
The show of the elements in the array:
1 1 3 4 4 4 5 5 6 6 8 8 8 8 9 9
The corresponding codes:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cstring>
using namespace std;
void generate_array(vector<int>& arr, int n);
int main()
{
cout << "The size of the array is " ;
int n;
cin >> n;
srand((unsigned)time(NULL));
vector<int> arr;
generate_array(arr, n);
for_each(arr.begin(), arr.end(), [](int x){ cout << setw(2) << x ;});
cout << endl;
sort(arr.begin(), arr.end());
cout << "The show of the elements in the array:" << endl;
for_each(arr.begin(), arr.end(), [](int x){ cout << setw(2) << x ;});
return 0;
}
void generate_array(vector<int>& arr, int n)
{
for (int i = 0; i < n; i++){
arr.push_back(rand() % 10);
}
}
The third steps to delete all the same elemets in array:
the final version of my results from my codes:
The size of the array is 14
3 9 7 9 4 3 6 6 5 6 3 9 6 3
The show of the elements in the array:
3 3 3 3 4 5 6 6 6 6 7 9 9 9
The size of deleted array: 6
The elements of the array after the deletion:
3 4 5 6 7 9
The corresponding codes:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cstring>
using namespace std;
void generate_array(vector<int>& arr, int n);
int removeDuplicates(vector<int>& arr);
int main()
{
cout << "The size of the array is " ;
int n;
cin >> n;
srand((unsigned)time(NULL));
vector<int> arr;
generate_array(arr, n);
for_each(arr.begin(), arr.end(), [](int x){ cout << setw(2) << x ;});
cout << endl;
sort(arr.begin(), arr.end());
cout << "The show of the elements in the array:" << endl;
for_each(arr.begin(), arr.end(), [](int x){ cout << setw(2) << x ;});
int end_pos = removeDuplicates(arr);
cout << "\nThe size of deleted array: " << end_pos << endl;\
cout << "The elements of the array after the deletion:" << endl;
for (int i = 0; i < end_pos; i++){
cout << setw(2) << arr[i] ;
}
return 0;
}
void generate_array(vector<int>& arr, int n)
{
for (int i = 0; i < n; i++){
arr.push_back(rand() % 10);
}
}
int removeDuplicates(vector<int>& arr)
{
int len = arr.size();
if (len == 0) return 0;
int i = 0; // index of left pointer log the position of new different elements
int j = 1; // index of right pointer log the fisrt position of new different elements
for (i, j; j < len; ){
if (arr[i] == arr[j]){
j++;
}else{
i++;
arr[i] = arr[j];
j++;
}
}
return i + 1;
}