# Find total number of distinct years from a string

Given a string containing the words and dates, the task is to find the total number of distinct years mentioned in that string.
Note: Assuming that the date will be in ‘DD-MM-YYYY’ format.

Examples:

```Input:  str = "UN was established on 24-10-1945.
India got freedom on 15-08-1947."
Output: 2
2 distinct years i.e. 1945 and 1947 have been referenced.

Input: str = "Soon after the world war 2 ended on 02-09-1945.
The UN was established on 24-10-1945."
Output: 1
Only 1 Year, i.e 1945 has been referenced .
```

Approach:

1. Start traversing the string.
2. Check if the current character is a digit. Store it in another string i.e. dateString.
3. Check if the current character is ‘-‘ then remove the characters stored in the dateString.
4. Check if the length of the dateString is equal to 4, then it means that is a year.
5. Store that year in an unordered_set.
6. Return the size of the unordered_set as it contains only unique values.

Below is the implementation of above approach:

## C++

 `// C++ Program to find the total ` `// number of distinct years ` `#include ` `using` `namespace` `std; ` ` `  `// function to find the total ` `// number of distinct years ` `int` `distinct_year(string str) ` `{ ` `    ``string str2 = ``""``; ` ` `  `    ``unordered_set uniqueDates; ` ` `  `    ``for` `(``int` `i = 0; i < str.length(); i++) { ` ` `  `        ``if` `(``isdigit``(str[i])) { ` `            ``str2.push_back(str[i]); ` `        ``} ` ` `  `        ``// if we found - then clear the str2 ` `        ``if` `(str[i] == ``'-'``) { ` `            ``str2.clear(); ` `        ``} ` ` `  `        ``// if length of str2 becomes 4 ` `        ``// then store it in a set ` `        ``if` `(str2.length() == 4) { ` `            ``uniqueDates.insert(str2); ` `            ``str2.clear(); ` `        ``} ` `    ``} ` ` `  `    ``// return the size of set ` `    ``return` `uniqueDates.size(); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"UN was established on 24-10-1945."` `                 ``"India got freedom on 15-08-1947."``; ` ` `  `    ``cout << distinct_year(str); ` ` `  `    ``return` `0; ` `} `

## Java

 `import` `java.util.HashSet; ` `import` `java.util.Set; ` ` `  `// Java Program to find the total  ` `// number of distinct years  ` `public` `class` `GFG { ` ` `  `// function to find the total  ` `// number of distinct years  ` `    ``static` `int` `distinct_year(String str) { ` `        ``String str2 = ``""``; ` ` `  `        ``Set uniqueDates = ``new` `HashSet<>(); ` ` `  `        ``for` `(``int` `i = ``0``; i < str.length(); i++) { ` ` `  `            ``if` `(Character.isDigit(str.charAt(i))) { ` `                ``str2 += (str.charAt(i)); ` `            ``} ` ` `  `            ``// if we found - then clear the str2  ` `            ``if` `(str.charAt(i) == ``'-'``) { ` `                ``str2 = ``""``; ` `            ``} ` ` `  `            ``// if length of str2 becomes 4  ` `            ``// then store it in a set  ` `            ``if` `(str2.length() == ``4``) { ` `                ``uniqueDates.add(str2); ` `                ``str2 = ``""``; ` `            ``} ` `        ``} ` ` `  `        ``// return the size of set  ` `        ``return` `uniqueDates.size(); ` `    ``} ` ` `  `// Driver code  ` `    ``static` `public` `void` `main(String[] args) { ` `        ``String str = ``"UN was established on 24-10-1945."` `                ``+ ``"India got freedom on 15-08-1947."``; ` ` `  `        ``System.out.println(distinct_year(str)); ` `    ``} ` `} `

## Python3

 `# Python3 Program to find the total ` `# number of distinct years ` ` `  `# function to find the total ` `# number of distinct years ` `def` `distinct_years(``str``): ` `    ``str2 ``=` `"" ` ` `  `    ``uniqueDates ``=` `set``() ` ` `  `    ``for` `i ``in` `range``(``len``(``str``)): ` `        ``if` `str``[i].isdigit(): ` `            ``str2 ``+``=` `str``[i] ` ` `  `        ``# if we found - then clear the str2 ` `        ``if` `str``[i] ``=``=` `'-'``: ` `            ``str2 ``=` `"" ` ` `  `        ``# if length of str2 becomes 4 ` `        ``# then store it in a set ` `        ``if` `len``(str2) ``=``=` `4``: ` `            ``uniqueDates.add(str2) ` `            ``str2 ``=` `"" ` ` `  `    ``# return the size of set ` `    ``return` `len``(uniqueDates) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``str` `=` `"UN was established on ``24``-``10``-``1945.``\ ` `           ``India got freedom on ``15``-``08``-``1947.``" ` ` `  `    ``print``(distinct_years(``str``)) ` ` `  `# This code is contributed by ` `# sanjeev2552 `

## C#

 `// C# Program to find the total  ` `// number of distinct years  ` `using` `System; ` `using` `System.Collections.Generic;  ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// function to find the total  ` `    ``// number of distinct years  ` `    ``static` `int` `distinct_year(String str) ` `    ``{ ` `        ``String str2 = ``""``; ` ` `  `        ``HashSet uniqueDates = ``new` `HashSet(); ` ` `  `        ``for` `(``int` `i = 0; i < str.Length; i++) ` `        ``{ ` `            ``if` `(``char``.IsDigit(str[i]))  ` `            ``{ ` `                ``str2 += (str[i]); ` `            ``} ` ` `  `            ``// if we found - then clear the str2  ` `            ``if` `(str[i] == ``'-'``) ` `            ``{ ` `                ``str2 = ``""``; ` `            ``} ` ` `  `            ``// if length of str2 becomes 4  ` `            ``// then store it in a set  ` `            ``if` `(str2.Length == 4)  ` `            ``{ ` `                ``uniqueDates.Add(str2); ` `                ``str2 = ``""``; ` `            ``} ` `        ``} ` ` `  `        ``// return the size of set  ` `        ``return` `uniqueDates.Count; ` `    ``} ` ` `  `    ``// Driver code  ` `    ``static` `public` `void` `Main(String[] args)  ` `    ``{ ` `        ``String str = ``"UN was established on 24-10-1945."` `+  ` `                     ``"India got freedom on 15-08-1947."``; ` ` `  `        ``Console.WriteLine(distinct_year(str)); ` `    ``} ` `} ` ` `  `// This code is contributed by Princi Singh `

Output:

```2
```

Time Complexity:O(n)

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.