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

  • Home
  • SEARCH
  • 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 9035085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:40:11+00:00 2026-06-16T08:40:11+00:00

Suppose we have an array in the size of n, with values range from

  • 0

Suppose we have an array in the size of n, with values range from 0 to n-1.
A function that validates that there are no duplications of values is needed, without using another array.

Prototype: bool UniqueValues(unsigned arr[], size_t n);

This is an algorithm question for job interviews, please don’t suggest using built-in std functions.

  • 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-16T08:40:12+00:00Added an answer on June 16, 2026 at 8:40 am

    Traverse the array and for each element, swap arr[i] with arr in the index of arr[i]( e.g, if arr[1]=2, swap it with arr[2], so arr[2]=2).
    The indication whether the value is duplicated is that arr[ arr[i] ] == arr[i]. (found 2 somewhere when there is already 2 at arr[2]).

    bool UniqueValues(unsigned arr[], size_t n)
    {
        int val;
        for(size_t i=0;i<n;i++)
        {
            val = arr[i];
            if(val == arr[val] )  { if( val != i) return false; }  //  If the element is duplicated
            else {
                swap( arr[i], arr[val] );  //  Move the element to the index matching its value
                val = arr[i];
                if(val == arr[val] )  return false;  //  Another check for the swapped element
            }
        }
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an array of values [a,b,c,d,...] and a function f(x,...) which returns
So let say I have an array of size 10 with index range from
Suppose you have a sorted range (x to y) of values in an array.
Suppose I have an array that mimics a database table. Each array element represents
I have a question about Quantifiers. Suppose that I have an array and I
Suppose that I have a 2D array (matrix) in Java like this... int[][] MyMat
Suppose I have an array similar to this (actual values probably will not be
Suppose I have an array of objects like this: var nodes = [{size:8,name:myName},{size:10,name:otherName},{size:11,name:userName}]; How
Suppose I have a C++ class with two private variables. A fixed size array,
Suppose i have an array $x = (31,12,13,25,18,10); I want to reduce this array

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.