给定一个数组,其中每个元素出现两次,除了一对(两个元素)。找到这个唯一对的元素。

输入:
第一行输入包含一个表示测试用例数的整数T。然后T测试用例如下。每个测试用例由两行组成。每个测试用例的第一行包含整数N表示数组的大小,第二行包含N个空格分隔元素。

输出:
对于每个测试用例, 在新行中以增加的顺序打印唯一对。

约束:
1 <= T <= 100 
1 <= N <= 10 3
1 <= A [i] <= 10 3

示例:
输入:


2 2 5 5 6 7 

1 3 4 1

输出:
6 7 
3 4

这个题算是最简单的一个题目,写下我的解题思路。

核心在于怎么找出来这一对元素:假设数组存放在b数组中,因为数组元素的大小是1-1000,所以新建数组a使其大小是1000,初始化为0。

依次将b中元素对应于a中的下标进行递增。如果最终a中是奇数的就是目标答案。只需要遍历一遍就可以了。

下面是具体代码实现:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n;//具体执行几次
    scanf("%d",&n);
    int *num=(int*)malloc(sizeof(int)*n);//num数组存放每个数组的大小.
    int i,j;//i是循环变量

    ;i<n;i++)
    {
        scanf("%d\n",&num[i]);//读入第i个数组元素的个数。
        int *b=(int*)malloc(sizeof(int)*num[i]);//分配b数组空间
        ;j<num[i];j++)
            scanf("%d",&b[j]);//依次读入数组元素.
        ]={};//初始化为0
        ;j<num[i];j++)
            a[b[j]-]++;//对数组a,b进行处理。

        ;j<;j++)
        {
            ==)
                printf());
        }
        printf("\n");
    }
    ;
}

如果存在任何不理解,请留言。

Find Unique pair in an array with pairs of numbers 在具有数字对的数组中查找唯一对的更多相关文章

  1. [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  2. 【leetcode】Merge Sorted Array(合并两个有序数组到其中一个数组中)

    题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assum ...

  3. [Swift]LeetCode34. 在排序数组中查找元素的第一个和最后一个位置 | Find First and Last Position of Element in Sorted Array

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  4. C#LeetCode刷题之#34-在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4970 访问. 给定一个按照升序排列的整数数组 nums,和一个目 ...

  5. 【LeetCode】1095. 山脉数组中查找目标值 Find in Mountain Array

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetco ...

  6. LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  7. 28.earch in Rotated Sorted Array(排序旋转数组中查找)

    Level:   Medium 题目描述: Suppose an array sorted in ascending order is rotated at some pivot unknown to ...

  8. Leetcode34.Find First and Last Position of Element in Sorted Array在排序数组中查找元素的位置

    给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...

  9. [LeetCode] K-diff Pairs in an Array 数组中差为K的数对

    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...

随机推荐

  1. SpringMVC中遇到页面跳转出现404错误的问题

    今天遇到了一个问题: 使用SpringMVC时,出现页面无法跳转的情况(404错误), 出现这个异常的原因在于SpringMVC的配置文件中控制器的配置书写错误: 原代码: <context:c ...

  2. 输入一个A和B,,A<=B,A>=1,B<=pow(10,18)计算F=B!/A!结果的最后一位

    *************************************************************************代理运行函数,判断结果,进行输出*********** ...

  3. c# AutoMapper 使用方式

    安装方式:使用vs自带的nuget管理工具,搜索AutoMapper ,选择第一个安装到你的项目即可. 我从网上找了一些资料,下载了个demo,然后自己又写了一遍,我把AutoMapper 的使用分为 ...

  4. Mybatis分页插件PageHelper的配置和使用方法

     Mybatis分页插件PageHelper的配置和使用方法 前言 在web开发过程中涉及到表格时,例如dataTable,就会产生分页的需求,通常我们将分页方式分为两种:前端分页和后端分页. 前端分 ...

  5. go语言常用开源库整理

    框架 https://github.com/go-martini/martini 图形验证码 https://github.com/dchest/captcha ORM https://github. ...

  6. Angular自定义组件实现数据双向数据绑定

    学过Angular的同学都知道,输入框通过[(ngModel)]实现双向数据绑定,那么自定义组件能不能实现双向数据绑定呢?答案是肯定的. Angular中,我们常常需要通过方括号[]和圆括号()实现组 ...

  7. 随机生成N个字符(包含数字和字母)

    '************************************************************* ' Name: GetRandomString ' Purpose: 随机 ...

  8. mysql 各数据类型的 大小及长度

    数字型 类型 大小 范围(有符号) 范围(无符号) 用途 TINYINT 1 字节 (-128,127) (0,255) 小整数值 SMALLINT 2 字节 (-32 768,32 767) (0, ...

  9. JAVA IO分析一:File类、字节流、字符流、字节字符转换流

    因为工作事宜,又有一段时间没有写博客了,趁着今天不是很忙开始IO之路:IO往往是我们忽略但是却又非常重要的部分,在这个讲究人机交互体验的年代,IO问题渐渐成了核心问题. 一.File类 在讲解File ...

  10. while(true)应用 之 实现自己的消息队列

    早些时候,一直有个疑问,就是比如你从前端发一个操作之后,后台为什么能够及时处理你的东西呢?当然了,我说的不是,服务器为什么能够立即接收到你的请求之类高大上的东西.而是,假设你用异步去做一个事情,而后台 ...