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

输入:
第一行输入包含一个表示测试用例数的整数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. memcached一致性哈希及php客户端实现

    1.memcached分布式算法 memcached的分布式是依靠客户端的算法来实现,假设键名为$key,服务器数量为N,常规的实现方式有两种: 取模哈希 crc32($key)%N,通过这个算法将键 ...

  2. C语言之循环计数

    #include<stdio.h>int main(){int num,count=0,i=0;scanf("%d",&num);num/=10;count++ ...

  3. Java面向对象回顾(1)

    世界万物皆对象. 面向对象四大特性:继承.封装.多态.抽象 Java中现有类,再有对象.创建对象(对象实例化)必须先创建类. 将对象的特征对应写成类的属性. 将对象的方法对应携程类的方法. 如何创建对 ...

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

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

  5. require.js模块化写法

    模块化 模块就是实现特定功能的一组方法.只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块. 下述两种写法等价 exports 对象是当前模块的导出对象,用于导出模块公有方法和属性. ...

  6. AspNet Core :创建自定义 EF Core 链接数据库

    这两天比较忙,写的会慢一点. 我们以控制台演示 EF Core的链接数据库 首先创建控制台程序 创建数据上下文类 EntityTable /// <summary> /// 继承 DbCo ...

  7. 物联网设备是如何被破解的?分析一种篡改IoT固件内容的攻击方式

    随着智能硬件进入到人们的生活,人们的生活质量开始有逐步的提高,人们与智能硬件之间的联系更加紧密.同时,智能硬件的安全问题也必须引起高度重视,因为其直接影响到人身安全.社会安全和国家安全.   大家是否 ...

  8. SFTP工具类 操作服务器

    package com.leadbank.oprPlatform.util;import com.jcraft.jsch.*;import com.jcraft.jsch.ChannelSftp.Ls ...

  9. mustache.js 使用

    对于mustache模板,我是属于即用即查的方法,以下记录仅是我常用的方式.方便以后使用时不用再去项目中去找,因为真的不好找.(此处 -->serious 脸) 当需要渲染一些数据列表的时候,使 ...

  10. sed命令针对文件操作具体解释

    Linux的简单shell脚本中改动文件操作 1.Sed简单介绍 sed 是一种在线编辑器,它一次处理一行内容.处理时.把当前处理的行存储在暂时缓冲区中,称为"模式空间"(patt ...