Loading
Lesson 10
Courses / Solving Leetcode Problems
Single Number - Leetcode 136 - Java

Learn how to solve the Leetcode problem of id 136, whose title is Single Number, using the Java programming language.

https://leetcode.com/problems/single-number

The Data Structures and Algorithms (DSA) lesson uses a bit manipulation approach to solve the problem.

The exclusive-OR (XOR) between two binary numbers is zero if they are the same and one if they are different.

Knowing that, you get the following properties: a XOR a is 0; a XOR 0 is a.

That is, a binary number XOR itself will yield zero.

A binary number XOR zero will yield the number itself.

In addition, knowing that properties such as Commutative and Associative allow the rearranging of terms as you write the elements in the array XOR'ed together, you conclude the following:

Duplicate numbers will cancel each other out and the single number will remain.

The time complexity for the solution is O(n) and its space complexity is O(1).

DSA problems are sometimes asked during tech job interviews for positions such as Software Engineer, so you can use the challenge to practice that skill.

No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: