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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:35:17+00:00 2026-06-12T10:35:17+00:00

I would like to return first (first met, from left to right) non repeating

  • 0

I would like to return first (first met, from left to right) non repeating element in an array.

i come with an algorithm that return smallest integer that is non repeating in an array quite easily, using only a array as extra space with length the max integer value in the array:

// smallest non repeating integer

int firstNonRepeatingInteger(int* input, int n)
{

     max = std::numeric_limits<int>::min() ;

     for(int i = 0 ; i < n ; i++)
     {
        if(input[i] > max)  
            max = input[i];
     }

     int* count = new int[max];

     for(int i = 0 ; i < n ; i++)
          count[input[i]] += 1 ;

     int j = 0;
     while(count[i] != 1)
       j++ ; 

     if(j < n)
        return input[count[j]] ;
     else
        return -1 ;

}

however, i cannot find an algorithm to find the first met, except having another n-array storing the time an integer is encountered.

any idea ? any other implementation of first algorithm?

thanks

  • 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-12T10:35:18+00:00Added an answer on June 12, 2026 at 10:35 am

    You almost had it:

    #include <limits>
    #include <iostream>
    
    int firstNonRepeatingInteger(int* input, int n)
    {
      int min = std::numeric_limits<int>::max() ;
      int max = std::numeric_limits<int>::min() ;
    
      // Find min/max values in input array.
      for(int i = 0; i < n; ++i)
      {
        if (input[i] > max)
          max = input[i];
        if (input[i] < min)
          min = input[i];
      }
    
      int* count;
      if (max - min + 1 > n)
      {
        count = new int[max - min + 1];
        // count has more elements than input, so only initialize
        // those elements which will be used.
        for(int i = 0; i < n; ++i)
          count[input[i] - min] = 0;
      }
      else
      {
        // Just initialize everything which is more efficient if
        // count has less elements than input.
        count = new int[max - min + 1]();
      }
    
      // Count number of occurrences.
      for(int i = 0; i < n; ++i)
        ++count[input[i] - min];
    
      // Find first non repeating element and return its index,
      // or -1 if there is none.
      int index = -1;
      for (int i = 0; i < n; ++i)
      {
        if (count[input[i] - min] == 1)
        {
          index = i;
          break;
        }
      }
      delete[] count;
      return index;
    }
    
    int main()
    {
      int values[5] = {-2, 4, 6, 4, -2};
      int index = firstNonRepeatingInteger(values, 5);
      if (index >= 0)
      {
        std::cout << "Found first non repeating integer " << values[index] <<
          " at index " << index << "." << std::endl;
      }
      else
        std::cout << "Found no non repeating integer in array." << std::endl;
      return 0;
    }
    

    Note that your code had several issues:

    • You never deleted the allocated memory.
    • new int[max] does not initialize the array with zeros. You need to use new int[max]() instead. Note the empty parentheses which will set all elements to zero (see ISO C++03 5.3.4[expr.new]/15).
    • Negative values in the input array will result in memory access violations.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to return the same response that I get from a different
I would like to return an array of string in my web services I've
I have a function that I would like to return 0 if a certain
I have a script with a factory method that I would like to return
I'm constructing a regex that is looking for dates. I would like to return
In a pylons controller, I would like to first return the response to the
I'm writing an Rcpp module an would like to return as one element of
I am building my first PHP/MySQL site and i would like to return search
I would like a method to return a Dictionary of Dictionary from an IQueryable
I would like to return the first letter of an NSString capitalized. I have

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.