Given **N** triangles along with the length of their three sides as **a, b and c**. The task is to count the number of unique triangles out of these **N** given triangles. Two triangles are different from one another if they have at least one of the sides different.

**Examples:**

Input:arr[] = {{3, 1, 2}, {2, 1, 4}, {4, 5, 6}, {6, 5, 4}, {4, 5, 6}, {5, 4, 6}};

Output:3

Input:arr[] = {{4, 5, 6}, {6, 5, 4}, {1, 2, 2}, {8, 9, 12}};

Output:3

This problem has been solved using ordered set of STL in the previous post.

**Approach:** We will be discussing the operator overloading based approach to solve this problem where we are going to overload the relational operator **(==) **of our class.

- Since any two sets of sides of a triangle, say {4, 5, 6}, {6, 5, 4}, are said to be equal if each element in one set corresponds to the elements in the other. So we will be checking each element of one set with the elements of the other set and keep a count of it. If the count will be the same, both the sets can simply be considered as equal. Now we have simply compared the sets using the relational operator to find the unique number of sets.
- To get the number of unique sets, we can follow an approach of comparing the current set’s uniqueness with the sets ahead of it. So, if there will be
**k**sets of the same type, only the last set would be considered to be unique.

Below is C++ implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Structure to represent a Triangle ` `// with three sides as a, b, c ` `struct` `Triangle { ` ` ` `int` `a, b, c; ` ` ` `public` `: ` ` ` `bool` `operator==(` `const` `Triangle& t) ` `const` `; ` `}; ` ` ` `// Function to overload relational ` `// operator (==) ` `bool` `Triangle::operator==(` `const` `Triangle& t) ` `const` `{ ` ` ` `int` `cnt = 0; ` ` ` `if` `((` `this` `->a == t.a) ` ` ` `|| (` `this` `->a == t.b) ` ` ` `|| (` `this` `->a == t.c)) { ` ` ` `cnt++; ` ` ` `} ` ` ` `if` `((` `this` `->b == t.a) ` ` ` `|| (` `this` `->b == t.b) ` ` ` `|| (` `this` `->b == t.c)) { ` ` ` `cnt++; ` ` ` `} ` ` ` `if` `((` `this` `->c == t.a) ` ` ` `|| (` `this` `->c == t.b) ` ` ` `|| (` `this` `->c == t.c)) { ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `// If all the three elements a, b, c ` ` ` `// are same, triangle is not unique ` ` ` `if` `(cnt == 3) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// For unique triangle return true ` ` ` `return` `true` `; ` `} ` ` ` `// Function returns the number ` `// of unique Triangles ` `int` `countUniqueTriangles(` `struct` `Triangle arr[], ` ` ` `int` `n) ` `{ ` ` ` ` ` `// Unique sets ` ` ` `int` `uni = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` ` ` `// Check on uniqueness for a ` ` ` `// particular set w.r.t others ` ` ` `int` `cnt = 0; ` ` ` ` ` `for` `(` `int` `j = i; j < n - 1; j++) { ` ` ` ` ` `// Checks if two triangles ` ` ` `// are different ` ` ` `if` `(arr[i] == arr[j + 1]) ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `// If count of unique triangles ` ` ` `// is same as the number of remaining ` ` ` `// triangles then, increment count ` ` ` `if` `(cnt == n - 1 - i) ` ` ` `uni++; ` ` ` `} ` ` ` ` ` `// Since last element that ` ` ` `// remains will be unique only ` ` ` `return` `uni + 1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// An array of structure to ` ` ` `// store sides of Triangles ` ` ` `struct` `Triangle arr[] = { ` ` ` `{ 3, 2, 2 }, { 3, 4, 5 }, { 1, 2, 2 }, ` ` ` `{ 2, 2, 3 }, { 5, 4, 3 }, { 6, 4, 5 } ` ` ` `}; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(Triangle); ` ` ` ` ` `// Function Call ` ` ` `cout << countUniqueTriangles(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** *O(N)*

**Auxiliary Space:** *O(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:

- Find number of unique triangles among given N triangles
- Count the number of possible triangles
- Count number of right triangles possible with a given perimeter
- Count the total number of triangles after Nth operation
- Count number of triangles possible for the given sides range
- Operator Overloading in C++
- C++ | Operator Overloading | Question 2
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 3
- C++ | Operator Overloading | Question 4
- C++ | Operator Overloading | Question 5
- C++ | Operator Overloading | Question 6
- C++ | Operator Overloading | Question 7
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 9
- Overloading New and Delete operator in c++
- Types of Operator Overloading in C++
- Increment (++) and Decrement (--) operator overloading in C++
- Matrix operations using operator overloading

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.