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 ...
随机推荐
- spring security 表单认证的流程
spring security表单认证过程 表单认证过程 Spring security的表单认证过程是由org.springframework.security.web.authentication ...
- as3 string split方法一些注意
split () 方法 AS3 function split(delimiter:*, limit:Number = 0x7fffffff):Array 如果指定 limit 参数,返回的数组中具有的 ...
- 10 并发编程-(线程)-GIL全局解释器锁&死锁与递归锁
一.GIL全局解释器锁 1.引子 在Cpython解释器中,同一个进程下开启的多线程,同一时刻只能有一个线程执行,无法利用多核优势 首先需要明确的一点是GIL并不是Python的特性,它是在实现Pyt ...
- 2 python第三章文件操作
1.三元运算 三元运算又称三目运算,是对简单的条件语句的简写,如: 简单条件语句: if 条件成立: val = 1 else: val = 2 改成三元运算: val = 1 if 条件成立 els ...
- c++ cout cin, 命名空间
cout<<a<<endl; cout<<a; 返回值其实就是一个输出流,(cout就是输出流) 上述语句等价于(cout<<a)<<end ...
- pwa 概念
- Spring MVC 接受的请求参数
目录 1. 概述 2. 详解 2.1 处理查询参数 2.2 处理路径参数接受输入 2.3 处理表单 3. 补充内容 3.1 Ajax/JSON 输入 3.2 multipart参数 3.3 接收 he ...
- log4j显示hibernate sql参数的配置
#下面的两条配置非常重要,设置为trace后,将可以看到打印出sql中 ? 占位符的实际内容 #this is the most important config for showing parame ...
- 初始化centoS 相关
install aspnetcoremodule for iis https://docs.microsoft.com/en-us/aspnet/core/publishing/iis?tabs=as ...
- Containerpilot 配置文件示例
{ consul: "localhost:8500", logging: { level: "INFO", format: "default" ...