Number of Unequal Triplets in Array
EasyUpdated: Aug 2, 2025
Practice on:
Problem
You are given a 0-indexed array of positive integers nums. Find the number of triplets (i, j, k) that meet the following conditions:
0 <= i < j < k < nums.lengthnums[i],nums[j], andnums[k]are pairwise distinct.- In other words,
nums[i] != nums[j],nums[i] != nums[k], andnums[j] != nums[k].
Return the number of triplets that meet the conditions.
Examples
Example 1
Input: nums = [4,4,2,4,3]
Output: 3
Explanation: The following triplets meet the conditions:
- (0, 2, 4) because 4 != 2 != 3
- (1, 2, 4) because 4 != 2 != 3
- (2, 3, 4) because 2 != 4 != 3
Since there are 3 triplets, we return 3.
Note that (2, 0, 4) is not a valid triplet because 2 > 0.
Example 2
Input: nums = [1,1,1,1,1]
Output: 0
Explanation: No triplets meet the conditions so we return 0.
Constraints
3 <= nums.length <= 1001 <= nums[i] <= 1000
Solution
Method 1 – Counting by Frequency
Intuition
We want to count the number of triplets (i, j, k) with i < j < k and all values distinct. Since n is small, we can count the frequency of each unique value, then for each combination of three different values, multiply their frequencies and sum up.
Approach
- Count the frequency of each unique value in nums.
- For each combination of three distinct values, multiply their frequencies and sum up.
- Return the total.
Code
C++
#include <unordered_map>
class Solution {
public:
int unequalTriplets(vector<int>& nums) {
unordered_map<int, int> freq;
for (int x : nums) freq[x]++;
vector<int> vals;
for (auto& [k, v] : freq) vals.push_back(v);
int n = vals.size(), ans = 0;
for (int i = 0; i < n; ++i)
for (int j = i+1; j < n; ++j)
for (int k = j+1; k < n; ++k)
ans += vals[i] * vals[j] * vals[k];
return ans;
}
};
Go
func unequalTriplets(nums []int) int {
freq := map[int]int{}
for _, x := range nums { freq[x]++ }
vals := []int{}
for _, v := range freq { vals = append(vals, v) }
n, ans := len(vals), 0
for i := 0; i < n; i++ {
for j := i+1; j < n; j++ {
for k := j+1; k < n; k++ {
ans += vals[i] * vals[j] * vals[k]
}
}
}
return ans
}
Java
import java.util.*;
class Solution {
public int unequalTriplets(int[] nums) {
Map<Integer, Integer> freq = new HashMap<>();
for (int x : nums) freq.put(x, freq.getOrDefault(x, 0) + 1);
List<Integer> vals = new ArrayList<>(freq.values());
int n = vals.size(), ans = 0;
for (int i = 0; i < n; i++)
for (int j = i+1; j < n; j++)
for (int k = j+1; k < n; k++)
ans += vals.get(i) * vals.get(j) * vals.get(k);
return ans;
}
}
Kotlin
class Solution {
fun unequalTriplets(nums: IntArray): Int {
val freq = nums.groupingBy { it }.eachCount().values.toList()
var ans = 0
val n = freq.size
for (i in 0 until n)
for (j in i+1 until n)
for (k in j+1 until n)
ans += freq[i] * freq[j] * freq[k]
return ans
}
}
Python
class Solution:
def unequalTriplets(self, nums: list[int]) -> int:
from collections import Counter
freq = list(Counter(nums).values())
ans = 0
n = len(freq)
for i in range(n):
for j in range(i+1, n):
for k in range(j+1, n):
ans += freq[i] * freq[j] * freq[k]
return ans
Rust
use std::collections::HashMap;
impl Solution {
pub fn unequal_triplets(nums: Vec<i32>) -> i32 {
let mut freq = HashMap::new();
for &x in &nums { *freq.entry(x).or_insert(0) += 1; }
let vals: Vec<_> = freq.values().cloned().collect();
let n = vals.len();
let mut ans = 0;
for i in 0..n {
for j in i+1..n {
for k in j+1..n {
ans += vals[i] * vals[j] * vals[k];
}
}
}
ans
}
}
TypeScript
class Solution {
unequalTriplets(nums: number[]): number {
const freq = new Map<number, number>()
for (const x of nums) freq.set(x, (freq.get(x) ?? 0) + 1)
const vals = Array.from(freq.values())
let ans = 0, n = vals.length
for (let i = 0; i < n; i++)
for (let j = i+1; j < n; j++)
for (let k = j+1; k < n; k++)
ans += vals[i] * vals[j] * vals[k]
return ans
}
}
Complexity
- ⏰ Time complexity:
O(n^3)(but n ≤ 100, so it's fast) - 🧺 Space complexity:
O(n)