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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:05:49+00:00 2026-06-01T11:05:49+00:00

Background: I have an N-length array of positive random numbers that are certain to

  • 0

Background:

I have an N-length array of positive random numbers that are certain to contain duplicates.
e.g. 10,4,5,7,10,9,10,9,8,10,5
Edit: N is likely to be 32, or some other power of two about that size.

The Problem:

I am trying to find the fastest way to replace the duplicates with the missing numbers from 0-(N-1). Using the above example, I want a result that looks like this:
10,4,5,7,0,9,1,2,8,3,6
The goal being to have one of each number from 0 to N-1, without just replacing all the numbers with 0-(N-1) (the random order is important).
Edit: It’s also important that this replacement is deterministic, i.e. the same input will have the same output (not random).

My solution:

Currently implemented in Java, uses 2 boolean arrays to keep track of used/unused numbers (unique numbers/missing numbers in the range [0,N) ), and has an approximate worst-case runtime of N+N*sqrt(N).
The code follows:

public byte[] uniqueify(byte[] input)
{
    boolean[] usedNumbers = new boolean[N];
    boolean[] unusedIndices = new boolean[N];
    byte[] result = new byte[N];

    for(int i = 0; i < N; i++) // first pass through
    {
        int newIdx = (input[i] + 128) % N; // first make positive
        if(!usedNumbers[newIdx]) // if this number has not been used
        {
            usedNumbers[newIdx] = true; // mark as used
            result[i] = newIdx; // save it in the result
        }
        else // if the number is used
        {
            unusedIndices[i] = true; // add it to the list of duplicates
        }
    }

    // handle all the duplicates
    for(int idx = 0; idx < N; idx++) // iterate through all numbers
    {
        if(unusedIndices[idx]) // if unused
            for(int i = 0; i < N; i++) // go through all numbers again
            {
                if(!usedNumbers[i]) // if this number is still unused
                {
                    usedNumbers[i] = true; // mark as used
                    result[i] = idx;
                    break;
                }
            }
    }
    return result;
}  

This seems like the fastest I can hope for, but I thought I’d ask the internet, because there are people much more clever than I who may have a better solution.

N.B. Suggestions/solutions do not have to be in Java.

Thank you.

Edit: I forgot to mention that I am converting this to C++. I posted my java implementation because it’s more complete.

  • 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-06-01T11:05:51+00:00Added an answer on June 1, 2026 at 11:05 am

    The fastest way to do this is probably the most straightforward one. I would take a pass through the list of data keeping a count of each distinct value and marking where duplicates appeared. Then it is just a matter of forming a list of unused values and applying them in turn at the places where duplicates were found.

    Tempting as it may be to use a C++ List, if speed is of the essence a simple C array is the most efficient.

    This program show the principle.

    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
      int data[] = { 10, 4, 5, 7, 10, 9, 10, 9, 8, 10, 5 };
      int N = sizeof(data) / sizeof(data[0]);
    
      int tally[N];
      memset(tally, 0, sizeof(tally));
    
      int dup_indices[N];
      int ndups = 0;
    
      // Build a count of each value and a list of indices of duplicate data
      for (int i = 0; i < N; i++) {
        if (tally[data[i]]++) {
          dup_indices[ndups++] = i;
        }
      }
    
      // Replace each duplicate with the next value having a zero count
      int t = 0;
      for (int i = 0; i < ndups; i++) {
        while (tally[t]) t++;
        data[dup_indices[i]] = t++;
      }
    
      for (int i = 0; i < N; i++) {
        cout << data[i] << " ";
      }
    
      return 0;
    }
    

    output

    10 4 5 7 0 9 1 2 8 3 6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: I have a kubuntu laptop right now that I can't use wirelessly, i.e.
Background: we have an application that generates reports from HTML (that may or may
I have an array of images in jquery that I am attempting to load
Little background. I have a navigation setup for when you click on a certain
I have some code that uses a multi-part form and HTML5 file object to
I have an array of unknown number of words, with an unknown max length.
I have a function on a background thread that looks like this: NSMutableArray *descriptions
Background I currently have an array like this: [1,1,2,3,4,5,5,5,6,7,8,8,8,8,9,10] I have been using a
Background: I have a unit of optimised Delphi/BASM routines, mostly for heavy computations. Some
Background: I have a little video playing app with a UI inspired by the

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.