Given three arrays a[], b[] and c[] of N elements representing the three sides of **N** triangles. The task is to find the number of triangles that are unique out of given triangles. A triangle is non-unique if all of its sides match with all the sides of some other triangle in length.

**Examples:**

Input:a[] = {1, 2}, b[] = {2, 3}, c[] = {3, 5}

Output:2

The triangles have sides 1, 2, 3 and 2, 3, 5 respectively.

None of them have same sides. Thus both are unique.

Input:a[] = {7, 5, 8, 2, 2}, b[] = {6, 7, 2, 3, 4}, c[] = {5, 6, 9, 4, 3}

Output:1

Only triangle with sides 8, 2 and 9 is unique.

**Approach:** The idea is, for each triangle, sort all of its sides and then store it in a map, if all those three sides are already present in the map then increase the frequency by 1, else its frequency will be 1. The count of elements of the map which have frequency 1 in the end will be the answer.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the number of unique triangles ` `int` `UniqueTriangles(` `int` `a[], ` `int` `b[], ` `int` `c[], ` `int` `n) ` `{ ` ` ` `vector<` `int` `> sides[n]; ` ` ` ` ` `// Map to store the frequency of triangles ` ` ` `// with same sides ` ` ` `map<vector<` `int` `>, ` `int` `> m; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Push all the sides of the current triangle ` ` ` `sides[i].push_back(a[i]); ` ` ` `sides[i].push_back(b[i]); ` ` ` `sides[i].push_back(c[i]); ` ` ` ` ` `// Sort the three sides ` ` ` `sort(sides[i].begin(), sides[i].end()); ` ` ` ` ` `// Store the frequency of the sides ` ` ` `// of the triangle ` ` ` `m[sides[i]] = m[sides[i]] + 1; ` ` ` `} ` ` ` ` ` `map<vector<` `int` `>, ` `int` `>::iterator i; ` ` ` ` ` `// To store the count of unique triangles ` ` ` `int` `count = 0; ` ` ` `for` `(i = m.begin(); i != m.end(); i++) { ` ` ` ` ` `// If current triangle has unique sides ` ` ` `if` `(i->second == 1) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 7, 5, 8, 2, 2 }; ` ` ` `int` `b[] = { 6, 7, 2, 3, 4 }; ` ` ` `int` `c[] = { 5, 6, 9, 4, 3 }; ` ` ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(` `int` `); ` ` ` ` ` `cout << UniqueTriangles(a, b, c, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `collections ` `import` `defaultdict ` ` ` `# Function to return the number ` `# of unique triangles ` `def` `UniqueTriangles(a, b, c, n): ` ` ` ` ` `sides ` `=` `[` `None` `for` `i ` `in` `range` `(n)] ` ` ` ` ` `# Map to store the frequency of ` ` ` `# triangles with same sides ` ` ` `m ` `=` `defaultdict(` `lambda` `:` `0` `) ` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` ` ` `# Push all the sides of the current triangle ` ` ` `sides[i] ` `=` `(a[i], b[i], c[i]) ` ` ` ` ` `# Sort the three sides ` ` ` `sides[i] ` `=` `tuple` `(` `sorted` `(sides[i])) ` ` ` ` ` `# Store the frequency of the sides ` ` ` `# of the triangle ` ` ` `m[sides[i]] ` `+` `=` `1` ` ` ` ` `# To store the count of unique triangles ` ` ` `count ` `=` `0` ` ` `for` `i ` `in` `m: ` ` ` ` ` `# If current triangle has unique sides ` ` ` `if` `m[i] ` `=` `=` `1` `: ` ` ` `count ` `+` `=` `1` ` ` ` ` `return` `count ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `[` `7` `, ` `5` `, ` `8` `, ` `2` `, ` `2` `] ` ` ` `b ` `=` `[` `6` `, ` `7` `, ` `2` `, ` `3` `, ` `4` `] ` ` ` `c ` `=` `[` `5` `, ` `6` `, ` `9` `, ` `4` `, ` `3` `] ` ` ` ` ` `n ` `=` `len` `(a) ` ` ` ` ` `print` `(UniqueTriangles(a, b, c, n)) ` ` ` `# This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

**Output:**

1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count number of Unique Triangles using Operator overloading
- Find the number of unique pairs satisfying given conditions
- Find maximum among all right nodes in Binary Tree
- Maximum sum of i*arr[i] among all rotations of a given array
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Generate two BSTs from the given array such that maximum height among them is minimum
- Maximum LCM among all pairs (i, j) from the given Array
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Maximum number of chocolates to be distributed equally among k students
- Find the longest substring with k unique characters in a given string
- Find Sum of all unique sub-array sum for a given array.
- Maximum number of unique values in the array after performing given operations
- Count of unique digits in a given number N
- Number of distinct ways to represent a number as sum of K unique primes
- Count the number of possible triangles
- Maximum value of XOR among all triplets of an array
- Maximize the maximum among minimum of K consecutive sub-arrays
- Minimum LCM and GCD possible among all possible sub-arrays
- Minimum value among AND of elements of every subset of an array
- Maximum XOR value of maximum and second maximum element among all possible subarrays

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.