
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- 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 out the number of pairs of equal substrings in Python
- Program to find number of good ways to split a string using Python
- Python Program to find out the number of matches in an array containing pairs of (base, number)
- Program to find array of doubled pairs using Python
- Program to find maximum score of a good subarray in Python
- Program to find number of pairs where elements square is within the given range in Python
- Program to maximize the number of equivalent pairs after swapping in Python
- Program to Find K-Largest Sum Pairs in Python
- C++ Program to find number of calling person pairs are calling
- Program to find number of quadruples for which product of first and last pairs are same in Python
- Program to count number of fraction pairs whose sum is 1 in python
- Program to find XOR sum of all pairs bitwise AND in Python

Advertisements