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 6533445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:07:35+00:00 2026-05-25T10:07:35+00:00

After reading this interesting question I was reminded of a tricky interview question I

  • 0

After reading this interesting question I was reminded of a tricky interview question I had once that I never satisfactorily answered:

You are given an array of n 32-bit unsigned integers where each element (except one) is repeated a multiple of three times. In O(n) time and using as little auxiliary space as possible, find the element of the array that does not appear a multiple of three times.

As an example, given this array:

1 1 2 2 2 3 3 3 3 3 3

We would output 1, while given the array

3 2 1 3 2 1 2 3 1 4 4 4 4

We would output 4.

This can easily be solved in O(n) time and O(n) space by using a hash table to count the frequencies of each element, though I strongly suspect that because the problem statement specifically mentioned that the array contains 32-bit unsigned integers that there is a much better solution (I’m guessing O(1) space).

Does anyone have any ideas on how to solve this?

  • 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-25T10:07:35+00:00Added an answer on May 25, 2026 at 10:07 am

    It can be done in O(n) time and O(1) space.

    Here is how you can do it with constant space in C#. I’m using the idea of “xor except with 3-state bits”. For every set bit, the “xor” operation increments the corresponding 3-state value.

    The final output will be the number whose binary representation has 1s in places that are either 1 or 2 in the final value.

    void Main() {
        Console.WriteLine (FindNonTriple(new uint[] 
                            {1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3} ));
        // 1
    
        Console.WriteLine (FindNonTriple(new uint[] 
                            {3, 2, 1, 3, 2, 1, 3, 2, 1, 4, 4, 4, 4} ));
        // 4
    }
    
    uint FindNonTriple(uint[] args) {
        byte[] occurred = new byte[32];
    
        foreach (uint val in args) {
            for (int i = 0; i < 32; i++) {
                occurred[i] = (byte)((occurred[i] + (val >> i & 1)) % 3);
            }
        }
    
        uint result = 0;
        for (int i = 0; i < 32; i++) {
            if (occurred[i] != 0) result |= 1u << i;
        }
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this article on thedailywtf.com, I'm not sure that I really got the
After reading this: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it I came to the conclusion that it is not valid
After reading this question , i saw the answer by Naveen containing a link
I was under the impression, after reading this article that it is better to
That title may need some work. Perhaps after reading this, you may be able
After reading and commenting on this question PHP Library for Keeping your site index
I've been wondering about this for a while after reading through an interesting article
After reading this SO question and noting the consensus about just how evil Thread.Sleep()
After reading this discussion and this article , I still have this question. Let's
After reading this question and through the various Phone Book sorting scenarios put forth

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.