Code Logo

Find Single Number

Published atDate not available
Easy 0 views
Like0

This problem feels like a little puzzle you can solve one step at a time. In Find Single Number, you are trying to work toward the right number by following one clear idea.

Find number that appears only once A good way to think about it is to first understand what goes in, then what rule you must follow, and finally what shape the answer should have.

For example, if the input is nums = [1], the answer is 1. Example with input: nums = [1] Another example is nums = [4,1,2,1,2], which gives 4. Example with input: nums = [4,1,2,1,2]

This is a friendly practice problem, but it still rewards careful reading. The key is understanding the rule clearly and then applying it carefully.

One helpful habit is to say the rule out loud in your own words before you start solving. If you can explain what counts, what changes, and what the final answer should look like, you are already much closer to the right solution.

Example Input & Output

Example 1
Input
nums = [1]
Output
1
Explanation

Example with input: nums = [1]

Example 2
Input
nums = [4,1,2,1,2]
Output
4
Explanation

Example with input: nums = [4,1,2,1,2]

Example 3
Input
nums = [2,2,1]
Output
1
Explanation

Example with input: nums = [2,2,1]

Algorithm Flow

Recommendation Algorithm Flow for Find Single Number
Recommendation Algorithm Flow for Find Single Number

Best Answers

java
class Solution {
    public int single_number(Object nums) {
        int[] arr = (int[]) nums;
        int result = 0;
        for (int num : arr) {
            result ^= num;
        }
        return result;
    }
}