Find one unique integer
On-Site Question 2 - SOLUTION
Problem
Given a list of account ID numbers (integers) which contains duplicates , find the one unique integer. (the list is guaranteed to only have one unique (non-duplicated) integer
Requirements¶
Do not use built-in Python functions or methods
Solution
This should feel very familiar to one of the problems we did in the array section of the course! We can use an XOR operation. The exclusive or operations will take two sets of bits and for each pair it will return a 1 value if one but not both of the bits is 1.
In Python we can use the ^ symbol to perform an XOR.
Now for our solution we can simply XOR all the integers in the list. We start with a unique id set to 0, then every time we XOR a new id from the list, it will change the bits. When we XOR with the same ID again, it will cancel out the earlier change.
By the end, we wil be left with the ID that was unique and only appeared once!
def solution(id_list):
# Initiate unique Id
unique_id = 0
# XOR fo revery id in id list
for i in id_list:
# XOR operation
unique_id ^= i
return unique_id
Good Job!
Find one unique integer的更多相关文章
- java的toString()及包装类的实现--Integer重点学习
1. toString()来源 2. toString()目的 3. toString()实现(JDK8) 1. toString()来源 源于java.lang.Object类,源码如下: /** ...
- neo4j-jersey分嵌入式和服务式连接图形数据库
原文载自:http://blog.csdn.net/yidian815/article/details/12887259 嵌入式: 引入neo4j依赖 <dependency> <g ...
- codeforces754D Fedor and coupons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Kafka设计解析(一)- Kafka背景及架构介绍
本文转发自Jason’s Blog,原文链接 http://www.jasongj.com/2015/01/02/Kafka深度解析 背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅 ...
- POJ1860 Currency Exchange(bellman-ford)
链接:http://poj.org/problem?id=1860 Currency Exchange Description Several currency exchange points are ...
- CF731C. Socks[DFS 贪心]
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
- Kafka深度解析
本文转发自Jason’s Blog,原文链接 http://www.jasongj.com/2015/01/02/Kafka深度解析 背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅 ...
- codeforces 754D. Fedor and coupons
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- VBA 检查模块中是否有某个函数
Function FindProcedures(ByRef wb As Workbook, ByVal Proc As String) As Boolean On Error GoTo Exit ...
- Mysql 唯一性约束添加
来自: http://blog.csdn.net/yumushui/article/details/38960619 一.单列唯一约束 在一列上添加唯一约束,主要是让该列在表中只能有唯一的一行,例如 ...
- Linux下类似windows下_beginthread和_endthread 的多线程开发
在 windows下头文件中包含 #include<process.h> 就可以使用_beginthread进行线程创建.个人感觉挺方便的. 在linux下类似于_beginthread ...
- 前端-javascript-ECMAScript5.0
-前端常用开发工具:sublime.visual Studio Code.HBuilder.Webstorm. 使用的PCharm跟WebStorm是JetBrains公司推出的编辑工具,开发阶段建议 ...
- getattr()函数详解
setattr(object,name,value): 作用:设置object的名称为name(type:string)的属性的属性值为value,属性name可以是已存在属性也可以是新属性. get ...
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderL
今天学习spring+cxf的时候遇到一个问题:在web.xml中配置了spring的上下文监听器: <listener> <listener-class>org.spring ...
- java-- 的子类/父类构造方法 转
前提:父类和子类同时都有有参的构造函数和无参构造函数. Java中的子类初始化时初值为空.调用顺序为先调用父类无参构造函数,然后调用子类无参构造函数. java中的子类初始化时初值不为空.调用顺序为先 ...
- scala spark rdd转数据框
1. http://blog.csdn.net/lw_ghy/article/details/51480358
- ssh 设置反向代理
远程主机上/etc/ssh/sshd_config中,开启 GatewayPorts yes systemctl reload sshd 本地: ssh -CqTnN -R 0.0.0.0:9000: ...
- 人机大战中AlphaGo及其执子人黄士杰
2016年3月9日注定要写入围棋界的历史.IT界的历史以及科学界的历史.当天,韩国著名围棋棋手李世石VS谷歌AlphaGo的人机大战赛在韩国首尔举行.对弈的一方为拥有1200多个处理器的谷歌人工智能系 ...