[Mảng 1 Chiều Cơ Bản]. Bài 8. Liệt kê các giá trị khác nhau

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 số nguyên A[] gồm N phần tử, hãy liệt kê các giá trị khác nhau trong mảng theo thứ tự xuất hiện, mỗi giá trị chỉ liệt kê 1 lần. Ở thời điểm hiện tại các bạn có thể duyệt trâu để giải bài này, sau này sẽ dùng cách tối ưu hơn.

Gợi ý :

Đối với mỗi chỉ số i trong mảng, dùng 1 vòng for con duyệt từ 0 tới i - 1 để kiểm tra xem có phần nào đứng trước i giống A[i] hay không, nếu không có thì in ra A[i]


Đầu vào

Dòng đầu tiên là số nguyên dương N

Dòng thứ 2 gồm N số nguyên viết cách nhau một vài khoảng trắng


Giới hạn

1<=N<=1000

-10^3<=A[i]<=10^3


Đầu ra

In ra các giá trị khác nhau theo thứ tự xuất hiện, mỗi giá trị chỉ liệt kê 1 lần.


Ví dụ :

Input 01
9 
1 2 3 4 1 2 3 4 5
Output 01
1 2 3 4 5
Input 02
9 
1 1 1 1 1 1 1 1 1
Output 02
1

Comments

Please read the guidelines before commenting.



  • 0
    kietne  commented on Dec. 12, 2025, 11:36 a.m.

    dùng set cho nó nhàn nha ae

    include <bits/stdc++.h>

    using namespace std;

    int main() { int n; cin >> n; vector<int> a(n); for (int &x : a) cin >> x;

    unordered_set<int> seen;
    for (int x : a) {
        if (seen.find(x) == seen.end()) {
            cout << x << " ";
            seen.insert(x);
        }
    }
    return 0;
    

    }


  • 1
    L8_DuongThanhKhiem  commented on Nov. 22, 2025, 4:07 a.m.

    FULL AC:

    #include <bits/stdc++.h>
    using namespace std;
    short n;
    vector <int> 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++) {
            bool found = false;
            for(int j = 0; j < i; j++) {
                if(a[i] == a[j]) {
                    found = true;
                    break;
                }
            }
            if(!found) cout << a[i] << " ";
        }
        return 0;
    }
    

  • 0
    quanpansa  commented on Oct. 2, 2025, 4:02 p.m.

    include<bits/stdc++.h>

    using namespace std;

    define ll long long

    long long b[1000009],a[1000009];

    int main(){ iosbase::syncwith_stdio(false); cin.tie(NULL);

    ll n;
    cin>>n;
    for(ll i=1;i<=n;i++){
        cin>>a[i];
    }
    for(ll i=1;i<=n;i++){
        if(b[a[i]]==0){ 
            cout<&lt;a[i]<<" ";
            b[a[i]]=1;
        }
    }
    

    }


  • -1
    naipret  commented on Sept. 15, 2025, 2:42 p.m.
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main() {
      ios::sync_with_stdio(false);
      cin.tie(nullptr);
    
      int num;
      cin >> num;
    
      vector<int> vec(num);
      for (int &ele : vec) {
        cin >> ele;
      }
    
      vector<bool> seen_ele(1000 * 2 + 1, false);
      for (int ele : vec) {
        if (seen_ele[ele + 1000] == false) {
          cout << ele << ' ';
          seen_ele[ele + 1000] = true;
        }
      }
    }
    

  • 0
    chau2010  commented on July 25, 2025, 12:13 a.m.

    void solve() { int n,t; cin>>n; int b[10005]; for(int i=0;i<n;i++){ cin>>t; b[t]++; if(b[t] == 1) { cout << t << ' '; } } }


  • 1
    id_07  commented on July 18, 2025, 3:27 p.m.

    Cach Lam: -duyet qua 2 vong for tao 1 bool de check neu phan tu kia trung lap thi se check qua true tuc la da co phan tu trung thi se bo qua

    
    #include <iostream>
    using namespace std;
    
    int main() {
        int n; cin >> n;
        int a[n];
        for(int i = 0; i < n; i++) cin >> a[i];
    
        for(int i = 0; i < n; i++) {
            bool found = false;
            for(int j = 0; j < i; j++) {
                if(a[i] == a[j]) {
                    found = true;
                    break;
                }
            }
            if(!found) cout << a[i] << " ";
        }
        return 0;
    }
    
    

  • -1
    hngoc  commented on July 7, 2025, 10:22 a.m.
    n=int(input())
    a=list(map(int,input().split()))
    b=[]
    for x in a:
        if not(x in b):
            b.append(x)
    for x in b:
        print(x,end=' ')
    

  • -1
    vanh14  commented on July 5, 2025, 4:35 p.m.

    include <algorithm>

    include <set>

    include <iostream>

    include<vector>

    using namespace std;

    int main() { int n,x; cin >> n; set<int> se; for (int i = 1; i <= n; i++) { cin >> x; se.insert(x); } for (int k : se) { cout << k << " "; } }


  • 0
    bengokyeuanh99  commented on May 28, 2025, 3:42 p.m.

    !/usr/bin/env python3

    -- coding: utf-8 --

    import sys

    def main(): input = sys.stdin.readline

    n = int(input())
    arr = list(map(int, input().split()))
    
    seen = set()
    for val in arr:
        if val not in seen:
            print(val, end=' ')
            seen.add(val)
    

    if name == "main": main()


  • 0
    NamKhanh187  commented on Feb. 14, 2025, 6:39 a.m.

    include <bits/stdc++.h>

    using namespace std; bool arr[1001] = {false}; int main() { iosbase::syncwith_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < n; i++) { int a; cin >> a; if (!(arr[a])) { cout << a << " "; arr[a] = true; } } return 0; }


  • 0
    duyvule  commented on Jan. 7, 2025, 12:54 p.m.

    include<bits/stdc++.h>

    using namespace std;

    int main() { int n; cin >> n; int a[n]; set<int> se; for (int i = 0;i < n;i++) { cin >> a[i]; se.insert(a[i]); } for (auto it : se) { cout << it << " "; }

    } sao ko đc nhỉ


    • 2
      fan_messi10  commented on Jan. 19, 2025, 3:37 p.m.

      in theo thứ tự ban đầu, ní sài set thì đã sắp xếp rồi


  • 0
    dangsp07  commented on Dec. 11, 2024, 8:00 a.m.

    include <iostream>

    using namespace std; int main() { int n; cin >> n; int a[n + 1], freq[10005] = {0};//freq[10005]: Một mảng đánh dấu tần suất xuất hiện của các số (giả sử các số nhập vào nằm trong khoảng 0 đến 10004). Ban đầu, toàn bộ các phần tử trong mảng freq được gán giá trị 0. for (int i = 1; i <= n; i++) { cin >> a[i]; if (!freq[a[i]]) { //Nếu freq[a[i]] == 0 (nghĩa là số a[i] chưa xuất hiện trước đó): in ra a[i] luôn Đánh dấu số a[i] đã xuất hiện bằng cách gán freq[a[i]] = 1. cout << a[i] << " "; freq[a[i]] = 1; } } return 0; }


  • -2
    Dungx_2008  commented on Nov. 25, 2024, 3:03 p.m.

    include <bits/stdc++.h>

    using namespace std; int a[100000]; int main() { int n; cin >> n; for(int x,i=1;i<=n;i++) { cin >> x; if(a[x] == 0) cout << x << " "; a[x] = 1; } return 0; }


  • -2
    khangsigma  commented on Oct. 10, 2024, 10:51 a.m.

    include <iostream>

    define N 10005

    using namespace std; int a[N]; int main() { int n;cin>>n; for (int i=1; i<=n; i++)cin>>a[i]; for (int i=1;i<=n;i++) {int kt = true; for (int j=1;j<i;j++) if (a[i]==a[j]) {kt = false; break;}

    if (kt) cout<<a[i]<<" ";} return 0; }


  • -1
    _nambuidinh_  commented on Aug. 26, 2024, 3:02 p.m.

    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]; } set<int> se; for(int i = 0; i < n; i++){ se.insert(a[i]); } //cout << se.size() << endl; for(auto x : se){ cout << x << " "; } }

    code như này có được kh mn?


  • -2
    tuyen_pham  commented on Aug. 19, 2024, 2:49 p.m.

    include <bits/stdc++.h>

    using namespace std; int a[1000000],b[1000000]; int main() { int n; cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; } for(int i=0;i<n;i++){ if(b[a[i]]==0){ cout<<a[i]<<" "; b[a[i]]++; } } return 0; }


  • -6
    pham_van_hung_CUS305467182  commented on Aug. 13, 2024, 9:42 a.m.

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


  • -13
    Lamlilac1234  commented on Aug. 5, 2024, 12:29 p.m.

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


  • -13
    theguy777_jaboi  commented on July 31, 2024, 1:50 a.m.

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


  • 0
    trieu_xuan_huy_DSA  commented on June 4, 2024, 2:03 p.m.

    //package javaapp;

    import java.util.Scanner;

    public class NewClass {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] duong = new int[1005];
        int[] am = new int[1005];
    
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
            if(a[i] >= 0){
                duong[a[i]] = 1;
            }
            else am[a[i]] = 1;
        }
    
        for(int i = 0; i < n; i++){
            if(am[a[i]] == 1){
                System.out.print(a[i] + " ");
                am[a[i]] = 0;
            }
            else if(duong[a[i]] == 1){
                System.out.print(a[i] + " ");
                duong[a[i]] = 0;
            }
        }
    
    }
    

    }


  • -5
    Itachi  commented on May 3, 2024, 4:25 a.m.

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