Find one unique integer

Posted Premiumlab

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Find one unique integer相关的知识,希望对你有一定的参考价值。

https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Mock%20Interviews/Social%20Network%20Company/Social%20Network%20Company%20-%20Interview%20Questions%20-%20SOLUTIONS/On-Site%20Question%202%20-%20SOLUTION.ipynb

 

 

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的主要内容,如果未能解决你的问题,请参考以下文章

leetcode1304. Find N Unique Integers Sum up to Zero

LeetCode --- 1304. Find N Unique Integers Sum up to Zero 解题报告

Leetcode1304. Find N Unique Integers Sum up to Zero

Leetcode 1304. Find N Unique Integers Sum up to Zero

Could not find result map java.lang.Integer] with root cause

1481. Least Number of Unique Integers after K Removals