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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:58:30+00:00 2026-06-18T13:58:30+00:00

This is my first post, hope it complies with posting guidelines of the site.

  • 0

This is my first post, hope it complies with posting guidelines of the site.
First of all a generic thanks to all the community: reading you from some months and learned a lot :o)

Premise: I’m a first years student of IT.

Here’s the question: I’m looking for an efficient way to count the number of unique pairs (numbers that appear exactly twice) in a given positive int array (that’s all I know), e.g. if:

int[] arr = {1,4,7,1,5,7,4,1,5};

the number of unique pairs in arr is 3 (4,5,7).

I have some difficulties in… evaluating the efficiency of my proposals let’s say.

Here’s the first code I did:

int numCouples( int[] v ) {
int res = 0;
int count = 0;
for (int i = 0 ; i < v.length; i++){
    count = 0;
    for (int j = 0; j < v.length; j++){
        if (i != j && v[i] == v[j]){
            count++;
        }
    }
    if (count == 1){
        res++;
    }
}
return res/2;
}

This shoudn’t be good cause it checks the whole given array as many times as the number of elements in the given array… correct me if I’m wrong.

This is my second code:

int numCouples( int[] v) {
int n = 0;
int res = 0;
for (int i = 0; i < v.length; i++){
    if (v[i] > n){
        n = v[i];
    }
}
int[] a = new int [n];
for (int i = 0; i < v.length; i++){
    a[v[i]-1]++;
}
for (int i = 0; i < a.length; i++){
    if (a[i] == 2){
        res++;
    }
}
return res;
}

I guess this should be better than the first one since it checks only 2 times the given array and 1 time the n array, when n is the max value of the given array. May be not so good if n is quite big I guess…

Well, 2 questions:

  1. am I understanding good how to “measure” the efficiency of the code?

  2. there’s a better way to count the number of unique pairs in a given array?

EDIT:
Damn I’ve just posted and I’m already swamped by answers! Thanks! I’ll study each one with care, for the time being I say I don’t get those involving HashMap: out of my knowledge yet (hence thanks again for the insight:o) )

  • 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-18T13:58:32+00:00Added an answer on June 18, 2026 at 1:58 pm

    Well, here’s another answer to your’s 2 questions:

    am I understanding good how to "measure" the efficiency of the code?
    

    There are various ways to measure efficiency of the code. First of all, people distinguish between memory efficiency and time efficiency. The usual way to count all these values is to know, how efficient are the building blocks of your algorithm. Have a look at the wiki.

    For instance, sorting using quicksort would need n*log(n) operations. Iterating through the array would need just n operations, where n is number of elements in the input.

    there's a better way to count the number of unique pairs in a given array?
    

    Here’s another solution for you. The complixity of this one would be: O(n*log(n)+n), where O(...) is Big O notation.

    import java.util.Arrays;
    
    public class Ctest {
      public static void main(String[] args) {
        int[] a = new int[] { 1, 4, 7, 1, 7, 4, 1, 5, 5, 8 };
        System.out.println("RES: " + uniquePairs(a));
      }
    
      public static int uniquePairs(int[] a) {
        Arrays.sort(a);
        // now we have: [1, 1, 1, 4, 4, 5, 5, 7, 7]
    
        int res = 0;
        int len = a.length;
        int i = 0;
    
        while (i < len) {
          // take first number
          int num = a[i];
          int c = 1;
          i++;
    
          // count all duplicates
          while(i < len && a[i] == num) {
            c++;
            i++;
          }
          System.out.println("Number: " + num + "\tCount: "+c);
          // if we spotted number just 2 times, increment result
          if (c == 2) {
            res++;
          }
        }
    
        return res;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post on the forum, hope all of you guys are
My first post on this site with huge hope:: I am trying to understand
this is my first post so I hope I am posting to the right
My first post here, hope I am following the posting guidelines right. If not,
this is my first post here so I hope I provide all the right
Hi this is my first post here hope you all are well. So Im
My first post here, so i hope this is the right area. I am
Edited at the request of commenters. I hope this is compliant. First post! Trying
This is my first time here so I hope I post this question at
This is my first post here so I hope you could help me with

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.