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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:12:15+00:00 2026-05-18T04:12:15+00:00

There are multiple related questions, but I’m looking for a solution specific to my

  • 0

There are multiple related questions, but I’m looking for a solution specific to my case. There is an array of (usually) 14 integers, each in the range of 1 to 34. How can I quickly tell if each int in a specific, static list appears at least once in this array?

For reference, I’m currently using this code, which was written to resemble the spec as closely as possible, so it can certainly be improved vastly:

if (array.Count < 13) {
    return;
}

var required = new int[] {
    0*9 + 1,
    0*9 + 9,
    1*9 + 1,
    1*9 + 9,
    2*9 + 1,
    2*9 + 9,                
    3*9 + 1,
    3*9 + 2,
    3*9 + 3,
    3*9 + 4,
    3*9 + 5,
    3*9 + 6,
    3*9 + 7,
};

IsThirteenOrphans = !required.Except (array).Any ();

The required list is not dynamic, i.e. it will always be the same during runtime. Using Linq is optional, the main aspect is performance.

Edit:

  • The input array is not sorted.
  • Input values may appear multiple times.
  • The input array will contain at least 14 items, i.e. 1 more than the required array.
  • There is only 1 required array and it is static.
  • The values in required are distinct.
  • You may assume that a histogram is cheap to create.

Update: I am also interested in a solution for a sorted input array.

  • 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-18T04:12:15+00:00Added an answer on May 18, 2026 at 4:12 am

    Idea 1
    If you need to compare with several required lists then you might sort the input list and then simply compare it by iterating. But sorting is of course not too fast, but not too slow either. But if you compare with several required lists the overhead of sorting might be amortized quickly.

    Once the array is sorted comparing is trivial:

    for(int i = 0; i < 14; i++)
      if(arr[i] != required[i]) return false;
    
    return true;
    

    Idea 2
    Or if the 14 integers are distinct/unique you can simply make required a HashSet and do

    input.Count(i => required.Contains(i)) == 14
    

    But I don’t know without actually testing it if that’s faster than sorting.

    Idea 3
    Calculate a quick hash which is invariant under permutations over the 14 ints and compare it to the known value of require. Only if the hash matches do the more expensive comparison.

    //Prepare/precalculated
    int[] hashes = new int[34];
    Random random = new Random();
    for(int i = 0; i < 34; i++)
      hashes[i] = random.NextInt();
    
    //On each list
    int HashInts(int[] ints)
    {
      int result = 0;
      foreach(int i in ints)
        result += hashes[i - 1];
    
      return result;
    }
    

    A wise choice of values for hashes might improve it a bit, but random values should be fine.

    Idea 4
    Create a Histogram:

    int[] CreateHistogram(int[] ints)
    {
      int[] counts = new int[34];
      foreach(int i in ints)
      {
        counts[i - 1]++;
      }
    
      return counts;
    }
    

    You can avoid the array creating by reusing an existing array if performance is really important.

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

Sidebar

Related Questions

There are multiple related questions, but I'm looking for a solution specific to my
Looking at the related questions, I don't think this specific question has been asked,
NOTE: There are other questions related to this on SO, but none of them
Although there are many related questions, I don't see one that addresses adding multiple
I know there are multiple posts on this but I still can't get it
I know there are multiple solutions online, but some are for windows, some are
I've searched over SO, but many questions are related to truncating the tables, but
There are related questions, such as How can I pass in 2 parameters to
I know there are many other posts on here with similar questions, but I
I know there are many queries related to this question but my problem is

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.