ได้รับอาเรย์ a[] พร้อมองค์ประกอบ งานคือพิมพ์การเกิดครั้งสุดท้ายขององค์ประกอบที่กำหนดในรายการ ไม่ได้แค่ลบซ้ำ แต่ต้องรักษาลำดับของการเกิดตามครั้งสุดท้ายที่เกิดขึ้น
เช่น อาเรย์ 6 องค์ประกอบมีค่าซ้ำ {1,3, 2, 3, 1, 2} ผลลัพธ์ควรเป็น 3 1 2
ตัวอย่าง
Input: a[]={4,2,2,4,1,5,1}
Output : 2 4 5 1

อัลกอริทึม
START Step 1-> Declare function void printelements(int a[], int n) Use STL unordered_mapele Loop For int i=0; i main() Declare array a[]={4,2,2,4,1,5,1} Declare int n=sizeof(a)/sizeof(a[0]) Call Function printelements(a,n) STOP
ตัวอย่างโค้ด C++
#includeusing namespace std; void printelements(int a[], int n) { unordered_map ele; for (int i = 0; i < n; i++) ele[a[i]] = i; for (int i = 0; i < n; i++) { if (ele[a[i]] == i) cout << a[i] << " "; } } int main() { int a[] = { 4,2,2,4,1,5,1 }; int n = sizeof(a) / sizeof(a[0]); printelements(a, n); return 0; }
ผลลัพธ์
การรันโปรแกรมจะได้ผลลัพธ์ดังนี้:
2 4 5 1