#include <stdio.h>
int main(void) {
int arrA[] = {1,2,3,4,2,3,5,6,1};
int length = sizeof(arrA)/sizeof(arrA[0]);
int arrB[length];
int arrC[length];
int k,p,s;
k=0;
p=0;
for (int i = 0; i < length; i++) {
// 記錄重複元素
for (int j = i + 1; j < length; j++) {
if (arrA[i] == arrA[j]) {
arrB[k]=arrA[i];
k=k+1;
}
}
// 記錄非重複元素
s=0;
for (int j = 0; j < length; j++) {
if (arrA[i] == arrA[j]) {
s=s+1;
}
if (j==(length-1)) {
if (s==1) {
arrC[p]=arrA[i];
p=p+1;
}
}
}
}
//
printf("重複元素 \n");
for(int i = 0; i < k; i++) {
printf("%d\n", arrB[i]);
}
//
printf("非重複元素 \n");
for(int i = 0; i < p; i++) {
printf("%d\n", arrC[i]);
}
return 0;
}
#include <stdio.h>
int arrayAppend(int arr[], int x, int idx);
void arrayPrint(int arr[], int len, char ch);
int main()
{
int X[] = {1,2,3,4,2,3,5,6,1};
size_t sz = sizeof(X) / sizeof(int);
int A[sz], B[sz];
int lenA = 0, lenB = 0, dup;
arrayPrint(X, sz, 'X');
for (int i=0;i<sz;i++) {
if (X[i]==-1) { continue; }
if (i==sz-1) {
lenB = arrayAppend(B, X[i], lenB);
} else {
dup = 0;
for (int j=i+1;j<sz;j++) {
if (X[j]==X[i]) {
dup = 1;
X[j] = -1;
}
}
if (dup) {
lenA = arrayAppend(A, X[i], lenA);
X[i] = -1;
} else {
lenB = arrayAppend(B, X[i], lenB);
}
}
}
arrayPrint(A, lenA, 'A');
arrayPrint(B, lenB, 'B');
return 0;
}
int arrayAppend(int arr[], int x, int idx) {
arr[idx++] = x;
return idx;
}
void arrayPrint(int arr[], int len, char ch) {
printf("%c -> ", ch);
for (int i=0;i<len;i++) {
printf("%d ", arr[i]);
}
printf("\n");
}