โปรแกรม C++ หาตัวเลขที่เกิดจำนวนครั้งคี่ในอาเรย์จำนวนเต็มบวกที่กำหนด ในอาเรย์นี้ ทุกตัวเลขเกิดจำนวนครั้งคู่
Input: arr[] = {5, 7, 8, 8, 5, 8, 8, 7, 7} Output: 7
คำอธิบาย
ใช้ลูปสองชั้น ชั้นนอกวนแต่ละองค์ประกอบ ชั้นในนับจำนวนครั้งที่เกิดขององค์ประกอบที่ชั้นนอกวน
ตัวอย่างโค้ด C++
#includeusing namespace std; int Odd(int arr[], int n){ for (int i = 0; i < n; i++) { int ctr = 0; for (int j = 0; j < n; j++) { if (arr[i] == arr[j]) ctr++; } if (ctr % 2 != 0) return arr[i]; } return -1; } int main() { int arr[] = {5, 7, 8, 8, 5, 8, 8, 7, 7}; int n = 9; cout <