Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6594173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:46:36+00:00 2026-05-25T17:46:36+00:00

Assume that the array has integers between 1 and 1,000,000. I know some popular

  • 0

Assume that the array has integers between 1 and 1,000,000.

I know some popular ways of solving this problem:

  1. If all numbers between 1 and 1,000,000 are included, find the sum of the array elements and subtract it from the total sum (n*n+1/2)
  2. Use a hash map (needs extra memory)
  3. Use a bit map (less memory overhead)

I recently came across another solution and I need some help in understanding the logic behind it:

Keep a single radix accumulator. You exclusive-or the accumulator with
both the index and the value at that index.

The fact that x ^ C ^ x == C is useful here, since each number will be
xor’d twice, except the one that’s in there twice, which will appear 3
times. (x ^ x ^ x == x) And the final index, which will appear once.
So if we seed the accumulator with the final index, the accumulator’s
final value will be the number that is in the list twice.

I will appreciate it if some one can help me understand the logic behind this approach (with a small example!).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:46:37+00:00Added an answer on May 25, 2026 at 5:46 pm

    Assume you have an accumulator

    int accumulator = 0;
    

    At each step of your loop, you XOR the accumulator with i and v, where i is the index of the loop iteration and v is the value in the ith position of the array.

    accumulator ^= (i ^ v)
    

    Normally, i and v will be the same number so you will end up doing

    accumulator ^= (i ^ i)
    

    But i ^ i == 0, so this will end up being a no-op and the value of the accumulator will be left untouched. At this point I should say that the order of the numbers in the array does not matter because XOR is commutative, so even if the array is shuffled to begin with the result at the end should still be 0 (the initial value of the accumulator).

    Now what if a number occurs twice in the array? Obviously, this number will appear three times in the XORing (one for the index equal to the number, one for the normal appearance of the number, and one for the extra appearance). Furthermore, one of the other numbers will only appear once (only for its index).

    This solution now proceeds to assume that the number that only appears once is equal to the last index of the array, or in other words: that the range of numbers in the array is contiguous and starting from the first index to be processed (edit: thanks to caf for this heads-up comment, this is what I had in mind really but I totally messed it up when writing). With this (N appears only once) as a given, consider that starting with

    int accumulator = N;
    

    effectively makes N again appear twice in the XORing. At this point, we are left with numbers that only appear exactly twice, and just the one number that appears three times. Since the twice-appearing numbers will XOR out to 0, the final value of the accumulator will be equal to the number that appears three times (i.e. one extra).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume that we have multiple arrays of integers. You can consider each array as
To simplify this posed question, assume that each cell has a Row Name and
Assume that int array arrayName is a member of class className, How can I
Assume that the core project has a base entity and every plugin maybe extends
Assume that I have this piece of code: @interface Foo : NSObject { Bar
Lets assume that I cant successfully access $_POST array but I do have the
Suppose that you're given an array A of n distinct elements drawn from some
I have a Member class that has an array of MemberRoles attached to it.
I have an array that created 5 objects. Each object has two strings and
Assume that I have an Array of objects in C#, and I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.