[Mảng 1 Chiều Cơ Bản]. Bài 34. Unique array

View as PDF

Submit solution

Points: 1.00 (partial)
Time limit: 1.0s
Memory limit: 256M
Input: stdin
Output: stdout

Author:
Problem source:
28Tech
Problem type
Allowed languages
C, C#, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Cho mảng A[] gồm N phần tử, nhiệm vụ của bạn là xóa các phần tử trong mảng sao cho không có 2 phần tử liền kề có giá trị giống nhau.

Ví dụ : mảng A[] = {1, 1, 2, 2, 2, 3, 3, 2, 1, 4} sau khi xóa sẽ được A[] = {1, 2, 3, 2, 1, 4}


Đầu vào

Dòng 1 gồm N

Dòng 2 gồm N số của mảng A[]


Giới hạn

1<=N<=1000

0<=A[i]<=100


Đầu ra

In ra mảng sau khi xóa


Ví dụ :

Input 01
11
2 3 3 0 0 0 3 3 3 3 4
Output 01
2 3 0 3 4

Comments

Please read the guidelines before commenting.



  • 0
    duclongtr2589  commented on Aug. 13, 2026, 12:34 p.m.

    Ủa bài này có z thôi mà nghĩ mãi mới ra huhu , toàn tự làm phức tạp vde ạ huhu

    include <bits/stdc++.h>

    using namespace std;

    int main(){

    int n; cin >> n;
    int a[n];
    
    for (int i = 0; i < n; i++){
        cin >> a[i];
    }
    
    cout << a[0] << " ";
    for (int i = 1; i < n; i++){
        if (a[i] != a[i-1]) cout << a[i] << " ";
    }
    
    return 0;
    

    }


  • 0
    ngocminh  commented on Aug. 13, 2026, 2:51 a.m.

    include<bits/stdc++.h>

    using namespace std;

    int a[100];

    int main(){

    int n; cin >> n;
    
    for(int i = 0; i < n; i ++){
    
        cin >> a[i];
    
    }
    
    cout << a[0] << " ";
    
    for(int i = 1; i < n; i++){
    
        if(a[i] != a[i-1]){cout << a[i] << " ";}
    
        if(a[i] == a[i-1]){}
    }
    
    return 0;
    

    }


  • 0
    Hoshino  commented on May 5, 2026, 8:50 a.m.

    include<bits/stdc++.h>

    using namespace std;

    define ll long long

    ll a[1123456],b[1123456],c[1123456],d1,d2,s,maxx=LLONGMIN,minn=LLONGMAX,maxx2=LLONGMIN,x,k,n,m; ll mark[1123456]; int main() { iosbase::syncwithstdio(false); cin.tie(nullptr); cin>>n; for(ll i=1; i<=n; i++) cin>>a[i]; for(ll i=1;i<=n;i+=1) { if(a[i]!=a[i+1]) cout<<a[i]<<" "; } return 0; } full AC nhe mn


  • 1
    Nguyen_Van1Phuc2Toan  commented on March 3, 2026, 2:22 p.m.

    include <iostream>

    using namespace std;

    int main(){ //Nhập int N; cin >> N; int A[N]; for(int i=0; i<N ; i++){ cin >> A[i]; }

    //Bài làm
    cout << A[0] << " ";
    for(int i=1; i&lt;N; i++){
        if(A[i] !=A[i-1]){
            cout << A[i] << " ";
        }
    }
    
    return 0;
    

    } Không nghĩ là cách này cũng đc nữa


  • 1
    L8_DuongThanhKhiem  commented on Nov. 20, 2025, 2:40 p.m.

    FULL AC:

    #include <iostream>
    #include <vector>
    using namespace std;
    short n;
    vector <short> a;
    int main() {
        cin >> n;
        a.resize(n);
        for (int i = 0; i < n; i++)cin >> a[i];
        for (int i = 0; i < n; i++) {
            if (a[i] != a[i + 1])cout << a[i] << " ";
        }
        return 0;
    }
    

  • 1
    hngoc  commented on July 4, 2025, 3:56 p.m.
    n=map(int,input().split())
    a=input().split()
    cur='-1'
    for x in a:
        if cur!=x:
            print(x,end=' ')
            cur=x
    

  • 1
    tandatha140904  commented on Feb. 16, 2025, 1:45 p.m.

    include <bits/stdc++.h>

    using namespace std;

    int main() { int n;cin>>n; int a[n], b[n]; for(int i=0;i<n;i++){ cin>>a[i]; } for(int i=0;i<n-1;i++){ if(a[i-1] == a[i] || a[i+1]== a[i]){ a[i] = -1; } } for(int i=0;i<n;i++){ if(a[i]>=0){ cout<<a[i]<<" "; } } return 0; }


  • 0
    VDev  commented on Jan. 17, 2025, 4:14 p.m.

    FULL AC

    #include <bits/stdc++.h>
    #include <iomanip>
    #include <cmath>
    #include <climits>
    #define ll long long
    using namespace std;
    ll a[10000011];
    ll cnt = 0, s = 0;
    
    int main(){
        ll n;
        cin >> n;
        for(ll i = 0; i < n; i++){
            cin >> a[i];
        }
        for(ll i = 0; i < n; i++){
                if(a[i] != a[i + 1]){
                    cout << a[i] << " ";
                }
        }
        return 0;
    }
    

  • -2
    nguyen_luong_an  commented on Dec. 18, 2024, 10:02 p.m.

    <3


  • -2
    theguy777_jaboi  commented on Nov. 15, 2024, 1:00 p.m.


  • -6
    theguy777_jaboi  commented on Nov. 15, 2024, 12:23 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -18
    khuemih  commented on Aug. 24, 2024, 2:12 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -1
    luan54bc  commented on Aug. 20, 2024, 1:31 p.m.

    include <bits/stdc++.h>

    using namespace std; void d(int a[], int n){ int i = 0; while ( i < n ){ while ( i < n - 1 && a[i] == a[i + 1]){ for ( int j = i; j < n - 1; j++){ a[j] = a[j + 1]; } n--; } i++; } } int main(){ int n; cin >> n; int a[n]; for ( int i = 0; i < n; i++){ cin >> a[i]; } d(a, n); for ( int i = 0; i < n; i++){ cout << a[i] << " "; } return 0; } tại sao em in ra output thì lúc trc đúng nhưng lúc gặp số 4 thì nó lại ra nhiều số 4 vậy ạ