合并两个数组,并忽略所有重复元素 [英] Merge two arrays and omit all repeating elements

查看:134
本文介绍了合并两个数组,并忽略所有重复元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数组.输入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}

I have 2 arrays. Input 1 and Input 2. Suppose input 1 has {1,1,2,3,3,4,5} and input 2 has {4,2,6,7,8}. Merged array {1,1,2,3,3,4,5,4,2,6,7,8}

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

After sorting my merged array looks like {1,1,2,2,3,3,4,4,5,6,7,8}

我的输出应类似于{5,6,7,8},因为它们是非重复元素.它应该既不会在第一时间发生两次,也不会在第一时间发生一次,而在第二时间发生过一次.

My output should be like {5,6,7,8} because those are non-repeating elements. It should have neither occurred twice in 1st nor once in 1st and once in 2nd.

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

I have put both the arrays into one single array(merged both). Sorted it and removed duplicates also. I am getting the output like {1,2,3,4,5,6,7,8} but i should not get 1,2,3,4 as they all have occurred twice in the merged array.

请帮助我尽快完成该程序.预先感谢.

Please help me to finish the program soon. Thanks in advance.

我不能使用结构.那就是我的任务.

I cant use structures. Thats my task.

#include<stdio.h>
#include<conio.h>
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;j<n-i-1;j++)
        {
            if(c[j]>c[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("%d r d rep.ele\n",c[i]);
        }
        else
        printf("%d r d ele\n",c[i]);
    }

    getch();
}

推荐答案

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

Let us solve this in an inappropriate, yet funny, way:

#include <stdio.h>

#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;
}

这篇关于合并两个数组,并忽略所有重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆