Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Identical Twins Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Identical Twins | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

For an array of integers nums, an identical twin is defined as pair (i, j) where nums[i] is equal to nums[j] and i < j. Given an array, find the number of identical twins.

Naive Approach

Using 2 nested loops, we iterate over all distinct pairs of elements and if they are equal we can increment the answer by one.

Analysis

  • Time Complexity: O(n2)
  • Space Complexity: O(1)

Implementation

C++
int getIdenticalTwinsCount(vector<int> &arr) {
   int countIdenticalTwins = 0;
       for (int i = 0; i < arr.size(); i++) {
       for (int j = i + 1; j < arr.size(); j++) {
           if (arr[i] == arr[j]) {
               countIdenticalTwins++;
           }
       }
   }
   return countIdenticalTwins;
}
Java
class Solution {
   int getIdenticalTwinsCount (int[] arr) {
       int countIdenticalTwins = 0;
       for (int i = 0; i < arr.length; i++) {
           for (int j = i + 1; j < arr.length; j++) {
               if (arr[i] == arr[j]) {
                   countIdenticalTwins++;
               }
           }
       }
       return countIdenticalTwins;
   }
}

Optimal Approach

Observe that we want the sum of a number of possible pairs of an element of the same type. This hints at the use of hashing.

We can hash the frequency of all the numbers in the list using a hashmap and for each distinct number, we can form Nc2 = (N * (N - 1) / 2) pairs of that particular number, where N is the frequency of that number in the list.

Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

Implementation

C++
int getIdenticalTwinsCount(vector<int> &arr) {
   unordered_map<int, int> frequency;
   for (auto x: arr) {
       frequency[x]++;
   }
   int identicalTwinCount = 0;
   for (auto x: frequency) {
       identicalTwinCount += (x.second * (x.second - 1) / 2);
   }
   return identicalTwinCount;
}
Java
class Solution {
   int getIdenticalTwinsCount (int[] arr) {
       HashMap<Integer, Integer> frequency = new HashMap<Integer, Integer>();
       for (int i = 0; i < arr.length; i++) {
           Integer val = frequency.get(arr[i]);
           if(val == null) {
               frequency.put(arr[i], 1);
           } else {
               frequency.put(arr[i], ++val);
           }
       }
       int identicalTwinCount = 0;
       for (Map.Entry<Integer, Integer> x: frequency.entrySet()) {
           identicalTwinCount += (x.getValue() * (x.getValue() - 1)) / 2;
       }
       return identicalTwinCount;
   }
}
Related Content
Clone List with Random Pointer
Cumulative Sum
Distinct Numbers in Window
Even Number of Digits
Four Sum
Implement Counting Sort
Largest Contiguous Sum
Longest Consecutive Sequence
Longest Subarray with Zero Sum
Longest Substring with K Unique Characters
Longest Substring Without Repeat
LRU Cache
Matrix Rotation
Max Consecutive Ones
Next Greater Permutation
Non-Repeating Element
Pascal's Triangle
Positive Cumulative Sum
Primes upto N
Primes upto N
Row Column Zero
Subarrays With Given XOR
Three Sum
Two Sum
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet