delete repeated elements in array

delete repeated elements in array,第1张

delete repeated elements in array

        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 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
void generate_array(vector& arr, int n);
int main()
{
    cout << "The size of the array is " ;
    int n;
    cin >> n;
    srand((unsigned)time(NULL));
    vector arr;
    generate_array(arr, n);
    for_each(arr.begin(), arr.end(), [](int x){ cout << setw(2) << x ;});
    return 0;
}
void generate_array(vector& 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 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
void generate_array(vector& arr, int n);
int main()
{
    cout << "The size of the array is " ;
    int n;
    cin >> n;
    srand((unsigned)time(NULL));
    vector 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& 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 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
void generate_array(vector& arr, int n);
int removeDuplicates(vector& arr);
int main()
{
    cout << "The size of the array is " ;
    int n;
    cin >> n;
    srand((unsigned)time(NULL));
    vector 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& arr, int n)
{
    for (int i = 0; i < n; i++){
        arr.push_back(rand() % 10);
    }
}
int removeDuplicates(vector& 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;
}

https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2gy9m/https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2gy9m/

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5636126.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存