Skip to content

Commit b0a938a

Browse files
committed
Add identical pairs problem
1 parent 41abcba commit b0a938a

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package java1.algorithms.array.identicalPairs;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class IdenticalPairs {
7+
private static int findIdenticalPairs(int[] nums){
8+
int count = 0;
9+
Map<Integer, Integer> frequencyMap = new HashMap<>();
10+
11+
for (int num : nums) {
12+
if(frequencyMap.containsKey(num)) {
13+
count += frequencyMap.get(num);
14+
}
15+
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) +1);
16+
}
17+
18+
return count;
19+
}
20+
public static void main(String[] args) {
21+
int[] nums1 = new int[]{6, 3, 1, 5, 3, 6, 5, 6};
22+
int[] nums2 = new int[]{2, 2, 2, 2};
23+
int[] nums3 = new int[]{1, 2, 3, 4};
24+
25+
System.out.println(findIdenticalPairs(nums1));
26+
System.out.println(findIdenticalPairs(nums2));
27+
System.out.println(findIdenticalPairs(nums3));
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
**Description:**
2+
Given an integer array `nums`, find the number of identical pairs.
3+
4+
**Note:** A pair `(i, j)` is called identical if `nums[i] == nums[j]` and `i < j`.
5+
6+
### Examples
7+
Example 1:
8+
9+
Input: nums = [6, 3, 1, 5, 3, 6, 5, 6]
10+
Output: 5
11+
12+
Example 2:
13+
14+
Input: nums = [2, 2, 2, 2],
15+
Output: 6
16+
17+
Example 2:
18+
19+
Input: nums = [1, 2, 3, 4],
20+
Output: 0
21+
22+
**Algorithmic Steps**
23+
This problem is solved with the help of array traversal and map for storing the frequency of each number. The algorithmic approach can be summarized as follows:
24+
25+
1. Create a function named `findIdenticalPairs`, which accepts input array(`nums`) to find the number of identical pairs.
26+
27+
2. Initialize a `count` variale to 0, which indicates the number of identical pairs, and a frequency map(`frequencyMap`) to count the occurrances of each number in an array.
28+
29+
3. Iterate over given array(`num`), add the the number of times a number appeared to count variable if the number has been appeared before.
30+
31+
4. Increment the current number appeared by 1.
32+
33+
5. Return `count` variable, which indicate the number of identical paris formed.
34+
35+
**Time and Space complexity:**
36+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an array. This is because we need to iterate over all the elements at most once for finding the identical pairs.
37+
38+
It takes constant time complexity of `O(n)` due to a map additional datastructure for storing the frequency of each number.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function findIdenticalPairs(nums){
2+
let count = 0;
3+
let frequencyMap = {};
4+
5+
for (const num of nums) {
6+
if(frequencyMap[num]) {
7+
count += frequencyMap[num];
8+
frequencyMap[num]++;
9+
} else {
10+
frequencyMap[num] = 1;
11+
}
12+
}
13+
14+
return count;
15+
}
16+
17+
const nums1 = [6, 3, 1, 5, 3, 6, 5, 6];
18+
const nums2 = [2, 2, 2, 2];
19+
const nums3 = [1, 2, 3, 4];
20+
21+
console.log(findIdenticalPairs(nums1));
22+
console.log(findIdenticalPairs(nums2));
23+
console.log(findIdenticalPairs(nums3));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
**Description:**
2+
Given an integer array `nums`, find the number of identical pairs.
3+
4+
**Note:** A pair `(i, j)` is called identical if `nums[i] == nums[j]` and `i < j`.
5+
6+
### Examples
7+
Example 1:
8+
9+
Input: nums = [6, 3, 1, 5, 3, 6, 5, 6]
10+
Output: 5
11+
12+
Example 2:
13+
14+
Input: nums = [2, 2, 2, 2],
15+
Output: 6
16+
17+
Example 2:
18+
19+
Input: nums = [1, 2, 3, 4],
20+
Output: 0
21+
22+
**Algorithmic Steps**
23+
This problem is solved with the help of array traversal and map for storing the frequency of each number. The algorithmic approach can be summarized as follows:
24+
25+
1. Create a function named `findIdenticalPairs`, which accepts input array(`nums`) to find the number of identical pairs.
26+
27+
2. Initialize a `count` variale to 0, which indicates the number of identical pairs, and a frequency map(`frequencyMap`) to count the occurrances of each number in an array.
28+
29+
3. Iterate over given array(`num`), add the the number of times a number appeared to count variable if the number has been appeared before.
30+
31+
4. Increment the current number appeared by 1.
32+
33+
5. Return `count` variable, which indicate the number of identical paris formed.
34+
35+
**Time and Space complexity:**
36+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an array. This is because we need to iterate over all the elements at most once for finding the identical pairs.
37+
38+
It takes constant time complexity of `O(n)` due to a map additional datastructure for storing the frequency of each number.

0 commit comments

Comments
 (0)