- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find number of good pairs in Python

Suppose we have an array nums. Here a pair (i,j) is said to be a good pair if nums[i] is same as nums[j] and i < j. We have to count the number of good pairs.

So, if the input is like nums = [5,6,7,5,5,7], then the output will be 4 as there are 4 good pairs the indices are (0, 3), (0, 4) (3, 4), (2, 5)

To solve this, we will follow these steps −

count:= 0

n:= size of nums

for i in range 0 to n - 1, do

for j in range i+1 to n - 1, do

if nums[i] is same as nums[j], then

count := count + 1

return count

## Example (Python)

Let us see the following implementation to get better understanding −

def solve(nums): count=0 n=len(nums) for i in range(n): for j in range(i+1,n): if nums[i] == nums[j]: count+=1 return count nums = [5,6,7,5,5,7] print(solve(nums))

## Input

[5,6,7,5,5,7]

## Output

4

- Related Questions & Answers
- Program to find number of good leaf nodes pairs using Python
- Program to find number of good triplets in Python
- Program to find max number of K-sum pairs in Python
- Program to find number of good ways to split a string using Python
- Program to find out the number of pairs of equal substrings in Python
- Python Program to find out the number of matches in an array containing pairs of (base, number)
- Program to find maximum score of a good subarray in Python
- Program to find array of doubled pairs using Python
- C++ Program to find number of calling person pairs are calling
- Program to find number of pairs where elements square is within the given range in Python
- Program to Find K-Largest Sum Pairs in Python
- Program to maximize the number of equivalent pairs after swapping in Python
- Program to find number of quadruples for which product of first and last pairs are same in Python
- Number of Equivalent Domino Pairs in Python
- Program to find XOR sum of all pairs bitwise AND in Python

Advertisements