PuzzlersWorld.com

  • Hacker Puzzle
  • Interview Puzzles
  • Number Puzzles
  • Maths Puzzles

Find Number present only once

(2 votes, average: 2.50 out of 5)

May 22, 2014 by puzzler 4 Comments

Write an algorithm to find out a number from an array of numbers where only one number occurs once and rest all occurs twice.

Note: Without using extra O(n) space and in O(n).

Click here to See SolutionHide Solution
XOR all the numbers ,you will get the number with single occurrence.

How?
XOR has these two properties,You can checkout the properties of XOR here.

  1. A (+)  A = 0
  2. A (+) 0 = A
  3. A (+) B (+) C = A (+) C (+) B

Thus all the numbers with two occurrences will become 0 after XOR and Number with only one occurrence will remain.

  • Share on Whatsapp
  • Share on Facebook
  • Share on Twitter
Facebook Comments
Next Puzzle
2nd smallest number from 32 numbers

Checkout more Interview Questions Tags: Snapdeal Interview Questions, Solved Puzzles

Comments

  1. mohd waseem says

    September 26, 2014 at 5:55 pm

    simply take xor of all the elements in the array,it will give the answer in one pass…

    Reply
  2. gaurav says

    June 22, 2014 at 4:20 am

    if the nujmber ocurring once is 0 then what

    Reply
  3. Mozart says

    June 16, 2014 at 5:56 pm

    I think this will work only if it is a sorted array,else it will take O(n^2) time.

    Reply
    • nomad says

      September 16, 2014 at 2:26 am

      Nope, will work either ways.

      Reply

Leave a Comment Cancel reply

Submit your Puzzle

You may also like

  • Basic Concepts C/C++
  • Security problem solution: Facebook hacker cup 2013 Round 1
  • Reverse a link list without using another
  • 3 SUM problem
  • Cookie Clicker Alpha Solution – Google Code Jam 2014
  • RoboElection solution: Facebook hacker cup 2013 Round 2
  • Rotate an array by k elements
  • Objective Questions set C/C++ – Part 4
  • Deceitful War Solution – Google Code Jam 2014
  • Find all non duplicate pairs that sum to S

Categories

  • Aive hi Puzzles
  • Akbar and Birbal
  • Alphabetical Puzzles
  • Bollywood Puzzles
  • Google Code jam
  • Hindi Riddles
  • Interview Puzzles
  • Interview Questions
  • Logical Puzzles
  • Malayalam Puzzles
  • Maths Puzzles
  • Miscellaneous
  • Number Puzzles
  • Picture Puzzles
  • Riddles
  • Tamil Puzzles
  • Technical

Social

  • View puzzlersworld’s profile on Twitter
privacy policy

Copyright © 2025 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in

  • Hacker Puzzle
  • Logo Puzzles
  • Optical Illusions
  • WhatsApp Puzzles
  • Picture Puzzles
  • Riddles
    ▼
    • Hindi Riddles
  • Bollywood Puzzles
  • Alphabetical Puzzles
  • Aive hi Puzzles
  • Interview Puzzles
  • Logical Puzzles
  • Interview Questions
    ▲
    • Data Structures
    • Binary Tree
    • Algorithms
    • Recursion Questions
    • Amazon Interview Questions
    • Snapdeal Interview Questions
    • Google Code jam
  • Technical
  • Akbar and Birbal
  • Number Puzzles
  • Maths Puzzles
  • Miscellaneous