solvingalgo
Published in

solvingalgo

Find the Single Number

Problem

Given an array of integers, every element appears thrice except for one which occurs once.Find that element which does not appear thrice.Note: Your algorithm should have a linear runtime complexity.Could you implement it without using extra memory?Example:Input: [1, 2, 4, 3, 3, 2, 2, 3, 1, 1]
Output: 4

Solving Process

Shift operator

00000101 << 1 = 00001010

AND operator

    00100101
AND 10100111
------------
00100101

OR operator

    00100101
OR 10100111
------------
10100111

Solution

Input: 5, 6, 5, 5
Output: 6
5 -> 101
6 -> 110
5 -> 101
5 -> 101
5 -> 101
6 -> 110
5 -> 101
5 -> 101
---
413
5 -> 101
6 -> 110
5 -> 101
5 -> 101
---
413
xx-
110 = 6

Further reading

Follow me on Twitter @teivah

--

--

A Collection of Helpful Programming Resources

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store