合并两个数组并省略所有重复元素

我有2个arrays。 输入1和输入2.假设输入1具有{1,1,2,3,3,4,5},输入2具有{4,2,6,7,8}。 合并数组{1,1,2,3,3,4,5,4,2,6,7,8}

排序后我的合并数组看起来像{1,1,2,2,3,3,4,4,5,6,7,8}

我的输出应该像{5,6,7,8},因为它们是非重复元素。 它应该既没有在第一次发生两次,也没有在第一次发生一次,在第二次发生一次

我已将两个数组放入一个单独的数组中(两者合并)。 对它进行排序并删除重复项。 我得到的输出像{1,2,3,4,5,6,7,8},但我不应该得到1,2,3,4,因为它们在合并的数组中都发生了两次。

请帮我尽快完成该计划。 提前致谢。

我不能使用结构。 多数民众赞成我的任务

#include #include main() { int a[10],b[10],c[10],i,j,n,n1,temp; clrscr(); printf("enter the no of ele in array1\n"); scanf("%d",&n); printf("enter 1st array elements\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("enter size of 2nd array"); scanf("%d",&n1); printf("enter 2nd array elements\n"); for(i=0;i<n;i++) { scanf("%d",&b[i]); } for(i=0;i<n;i++) { c[i]=a[i]; } j=0; for(i=n;i<n+n1;i++) { c[i]=b[j]; // printf("\n2nd array values are %d",c[i]); j++; } for(i=0;i<n+n1;i++) { printf("%d\n",c[i]); } for(i=0;i<n-1;i++) { for(j=0;jc[j+1]) { temp=c[j]; c[j]=c[j+1]; c[j+1]=temp; } } } for(j=0;j<n+n1;j++) { printf("sorted is %d\n",c[j]); } for(i=0;i<n+n1;i++) { if(c[i]==c[i+1]) { temp=c[i]; // printf("%drd rep.ele\n",c[i]); } else printf("%drd ele\n",c[i]); } getch(); } 

让我们以一种不恰当但又有趣的方式解决这个问题:

 #include  #define MAXINPUT 10 void pack(const int* k, int n, int* table) { for(int i = 0; i < n; ++i) { table[k[i]]++; } } void dump(const int* table) { int flag = 0; for(int i = 0; i < MAXINPUT; ++i) { if(table[i] == 1) { printf("%s%d", (flag) ? ", " : "", i); flag = 1; } } } int c[MAXINPUT]; int main(void) { const int a[] = { 1, 1, 2, 3, 3, 4, 5}; const int b[] = { 4, 2, 6, 7, 8}; pack(&a[0], sizeof(a)/sizeof(int), &c[0]); pack(&b[0], sizeof(b)/sizeof(int), &c[0]); dump(&c[0]); return 0; } 
 size_t i, non_repeating_index = 0; // allocate memory to hold the new array int * non_repeating_elements = calloc(sizeof(int), merged_array_length); // check if the first element is unique if (merged_array_length >= 2 && merged_array[0] != merged_array[1]) non_repeating_elements[non_repeating_index++] = merged_array[0]; // for all other elemnts check if it equals the ones around it for (i = 1; i < merged_array_length -1; i++) { if (merged_array[i-1] != merged_array[i] && merged_array[i] != merged_array[i + 1]) non_repeating_elements[non_repeating_index++] = merged_array[i]; } // check the last element if (merged_array_length >= 3 && merged_array[i-1] != merged_array[i]) non_repeating_elements[non_repeating_index++] = merged_array[i]; // resize the array to conserve memory non_repeating_elements = realloc(non_repeating_elements, sizeof(*non_repeating_elements) * non_repeating_index); 

你快到了! 不要试图想要花哨,只需创建一个包含两个int字段的结构:value和count。 创建这些结构的数组并遍历排序列表。 最后,遍历结构数组并选择相应计数为1的所有值。

有一些细节可以解决,比如你的结构数组应该有多大,但是嘿,你显然不怕编码。 如果arrays比你需要的更大,那么…只要它足够大就关心谁?

如果你已经对Arrays进行了排序和合并,那么它非常简单:只需与前任和后继者进行比较即可。 但是需要一些额外的逻辑才能超出范围。

 #include  int main(int argc, char** argv){ const int sortedMergedArray[] = {1,1,2,2,3,3,4,4,5,6,7,8}; const size_t mergedArrayLength = 12; for(size_t i=0; i= 1) predecessor = sortedMergedArray[i-1]; if(i <= mergedArrayLength-1) successor = sortedMergedArray[i+1]; if(sortedMergedArray[i] != successor && sortedMergedArray[i] != predecessor){ printf("%d, ", sortedMergedArray[i]); } } return 0; } 

输出:

 5, 6, 7, 8, 
 #include  #include  #include  #include  #define NUMCMP(x,y) (((x) < (y)) ? -1 : ((x) > (y)) ? 1 : 0) #define numcmp(type) int cmp_##type(const void *a, const void *b){\ type x = *(type*)a;\ type y = *(type*)b;\ return NUMCMP(x,y);\ } numcmp(int);//define function cmp_int void print_array(int a[], size_t size){ size_t i; printf("{"); for(i=0;i 
  #include int main() { int a[30],b[30]; int n,k,i,j,p,c=0; printf("How many element in first array\n"); scanf("%d",&n); printf("Enter elements"); for(i=0;i