---
# System prepended metadata

title: 2475. Number of Unequal Triplets in Array
tags: [Leetcode, Medium]

---

# 2475. Number of Unequal Triplets in Array
###### tags: `Leetcode` `Medium`
Link: https://leetcode.com/problems/number-of-unequal-triplets-in-array/description/
## 思路
首先用cntMap计算出每个数字出现的频率
对于数字 a,b,c 有cntMap[a], cntMap[b], cntMap[c]个unequal triplets
因此我们可以遍历map 然后记录前(```l```)后(```r```)各有多少元素 对于每个元素 ```ans+=l*r*cntMap.get(key)```
## Code
```java=
class Solution {
    public int unequalTriplets(int[] nums) {
        int ans = 0;
        Map<Integer, Integer> cntMap = new HashMap<>();
        for(int num:nums) cntMap.put(num, cntMap.getOrDefault(num,0)+1);
        int l=0, r = nums.length;
        for(int key:cntMap.keySet()){
            r -= cntMap.get(key);
            ans += l*r*cntMap.get(key);
            l += cntMap.get(key);
        }
        return ans;
    }
}
```