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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:10:47+00:00 2026-05-18T03:10:47+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. How can I quickly tell if each int appears exactly twice (i.e. there are 7 pairs)? The value range is from 1 to 35. The main aspect here is performance.

For reference, this is my current solution. It was written to resemble the spec as closely as possible and without performance in mind, so I’m certain is can be improved vastly:

var pairs = Array
    .GroupBy (x => x)
    .Where (x => x.Count () == 2)
    .Select (x => x.ToList ())
    .ToList ();
IsSevenPairs = pairs.Count == 7;

Using Linq is optional. I don’t care how, as long as it’s fast 🙂

Edit: There is the special case that an int appears 2n times with n > 1. In this case the check should fail, i.e. there should be 7 distinct pairs.

Edit: Result
I tested Ani’s and Jon’s solutions with tiny modifications and found during multiple benchmark runs in the target app that Ani’s has about twice Jon’s throughput on my machine (some Core 2 Duo on Win7-64). Generating the array of ints already takes about as long as the respective checks, so I’m happy with the result. Thanks, all!

  • 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-18T03:10:48+00:00Added an answer on May 18, 2026 at 3:10 am

    Clearly, LINQ won’t provide the optimal solution here, although I would improve your current LINQ solution to:

    // checks if sequence consists of items repeated exactly once
    bool isSingleDupSeq = mySeq.GroupBy(num => num)
                               .All(group => group.Count() == 2);
    
    // checks if every item comes with atleast 1 duplicate
    bool isDupSeq = mySeq.GroupBy(num => num)
                         .All(group => group.Count() != 1);
    

    For the specific case you mention (0 – 31), here’s a faster, array-based solution. It doesn’t scale very well when the range of possible numbers is large (use a hashing solution in this case).

    // elements inited to zero because default(int) == 0
    var timesSeenByNum = new int[32];
    
    foreach (int num in myArray)
    {
        if (++timesSeenByNum[num] == 3)
        {
            //quick-reject: number is seen thrice
            return false;
        }
    }
    
    foreach (int timesSeen in timesSeenByNum)
    {
        if (timesSeen == 1)
        {
            // only rejection case not caught so far is
            // if a number is seen exactly once
            return false;
        }
    }
    
    // all good, a number is seen exactly twice or never
    return true;   
    

    EDIT: Fixed bugs as pointed out by Jon Skeet. I should also point out that his algo is smarter and probably faster.

    • 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
Is there a way to fall through multiple case statements without stating case value:
There are multiple values I have been storing in ASP.NET configSections sections for each
There are multiple ways to fill an NSTableView up with data through either bindings,
I have a project where there are multiple applications that have some common configuration
I'm implementing a tagging system for a website. There are multiple tags per object
This is sort of a follow-up to this question . If there are multiple
There's a common way to store multiple values in one variable, by using a
Is there a way to search for multiple strings simultaneously in Vim? I recall
Is there a difference between generating multiple numbers using a single random number generator

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.