Skip to main content
Back to problems
#2475
Easy Algorithms

Number of unequal triplets in array

Array Hash Table Sorting
73.3% acceptance
Feb 25, 2026
451
49
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.length nums[i], nums[j], and nums[k] are pairwise distinct. Return the number of triplets that meet the conditions.

Solution

Rust
Time O(n³)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn unequal_triplets(nums: Vec<i32>) -> i32 {
    let n = nums.len();
    let mut count = 0;
    for i in 0..n - 2 {
      for j in i + 1..n - 1 {
        if nums[i] == nums[j] { continue; }
        for k in j + 1..n {
          if nums[i] != nums[k] && nums[j] != nums[k] {
            count += 1;
          }
        }
      }
    }
    count
  }
}