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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:38:39+00:00 2026-06-04T22:38:39+00:00

I have got ‘n’ rows of data sets, each data set having two components

  • 0

I have got ‘n’ rows of data sets, each data set having two components separated by space. First is card number and second is name. A person is same, if he has same card number or the name. How to find the total number of unique person from the data set?

Example:

1 A

1 B

2 B

3 C

This data set has 2 unique person. This is because the first and second row card number is same and second and third row name is same.

What kind of algorithms can be used to solve this kind of problem?

  • 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-04T22:38:42+00:00Added an answer on June 4, 2026 at 10:38 pm

    The solution I came up with is using some sort of partitioning: Most operation are done in O(1) or O(logn) and it is done once by user, therefor the time complexity is about O(n),O(logn) depending on how Dictionary are implemented.

    int CountUnique(Person persons)
    {
            Dictionary<string, int> phones = new Dictionary<string, int>(); //Keep a dictionary where each phone number is mapped to a partition
            Dictionary<string, int> email = new Dictionary<string, int>();  //Keep a dictionary where each email is mapped to a partition
            bool[,] linked = new bool[n, n]; //Lookup table used to tell if 2 partitions are linked (represents the same person)
            int count = 0;
            int max = 0;
            for (int i = 0; i < n; i++)
            {
                Person p = persons[i];
                int pA = -1, pB = -1; // Partition found using the phone number, Partition found using email
                if (phones.ContainsKey(p.Phone))
                {
                    pA = phones[p.Phone];
                }
                if (emails.ContainsKey(p.Email))
                {
                    pB = emails[p.Email];
                }
    
                if (pA == -1 && pB == -1) // First case, not found: Add both phones and email and create a new partition. Number of unique persons is also incremented.
                {
                    phones.Add(p.Phone.Trim(), max);
                    emails.Add(p.Email.Trim().ToLower(), max);
                    max++;
                    count++;
                }
                else
                {
                    if (pA != -1 && pB != -1 && pA != pB) // Found using both parameters on different partitions
                    {
                        if (!linked[pA, pB] && !linked[pB, pA]) // If the partition are not linked, link them
                        {
                            count--; // We'lost one partition => one unique person less
                            linked[pA, pB] = linked[pB, pA] = true;
                        }
                    }
                    if (pA == -1) // We did find an existing email but no phone
                    {
                        phones.Add(p.Phone.Trim(), pB); // Add the phone number
                        max++;
                    }
                    if (pB == -1) // We did find an existing phone but no email
                    {
                        emails.Add(p.Email.Trim().ToLower(), pA); // Add the email number
                        max++;
                    }
                }
    
            }
    
    
    
    return count;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got two movieclips on the main timeline. First one is envelope_mc and
I have got two pages. example.com/php.com and example.com. I use this function to set
I have got following folder structure for my domain (domain.com): /data/images (it´s subdomain name,
I have got my own JAR () running via separated class loaders and one
I have got two sections with billing and delivery address within a datalist: <asp:DataList
I have got a bit of a problem. I have two pages (Thumbnail.aspx &
I have got a field in mysql table with the type SET and collation
I have got an history page, which is a UItableview with 5 rows. I
I have got below two public properties in my DataContract class. I want to
I have got quite slow query (actually this is data loading from mysql.dump). Now

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.