# Print all 3 digit repeating numbers in a very large number

Given a very large number, print all the 3 digit repeating numbers with their frequency. If a 3 digit number appears more then once, print the number and its frequency.

Example:

```Input: 123412345123456
Output: 123 - 3 times
234 - 3 times
345 - 2 times

Input: 43243243
Output: 432 - 2 times
324 - 2 times
243 - 2 times
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Since the number is very large, it is stored in a string. Initially, the first three digit number will be the first three characters from the left. Iterate in the string from the 3rd index from the left in the string and do %100 to remove the first character and append the ith index number at the end to get the new number. Increase the frequency of the number in the hash map. In the end, when all the 3 digit numbers are generated, print all the numbers which have a frequency of more than 1.

Below is the implementation of the above idea:

## C++

 `// CPP program to print 3 digit repeating numbers ` `#include ` `using` `namespace` `std; ` ` `  `// function to print 3 ` `// digit repeating numbers ` `void` `printNum(string s) ` `{ ` `    ``int` `i = 0, j = 0, val = 0; ` ` `  `    ``// Hashmap to store the ` `    ``// frequency of a 3 digit number ` `    ``map <``int``, ``int``> mp; ` ` `  `    ``// first three digit number ` `    ``val = (s[0] - ``'0'``) * 100 ` `            ``+ (s[1] - ``'0'``) * 10 ` `            ``+ (s[2] - ``'0'``); ` ` `  `    ``mp[val] = 1; ` `    ``for` `(i = 3; i < s.length(); i++) { ` `        ``val = (val % 100) * 10 + s[i] - ``'0'``; ` ` `  `        ``// if key already exists ` `        ``// increase value by 1 ` `        ``if` `(mp.find(val) != mp.end()) { ` `            ``mp[val] = mp[val] + 1; ` `        ``} ` `        ``else` `{ ` `            ``mp[val] = 1; ` `        ``} ` `    ``} ` ` `  `    ``// Output the three digit numbers with frequency>1 ` `    ``for` `(``auto` `m : mp) { ` `        ``int` `key = m.first; ` `        ``int` `value = m.second; ` `        ``if` `(value > 1) ` `            ``cout << key  << ``" - "` `<< value << ``" times"` `<< endl; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main()  ` `{ ` `    ``// Input string ` `    ``string input = ``"123412345123456"``; ` `  `  `    ``// Calling Function ` `    ``printNum(input); ` `} ` `// This code is contributed by Nishant Tanwar `

## Java

 `// Java program to print 3 digit repeating numbers ` `import` `java.util.*; ` `import` `java.lang.*; ` ` `  `public` `class` `GFG { ` ` `  `    ``// function to print 3 ` `    ``// digit repeating numbers ` `    ``static` `void` `printNum(String s) ` `    ``{ ` `        ``int` `i = ``0``, j = ``0``, val = ``0``; ` ` `  `        ``// Hashmap to store the ` `        ``// frequency of a 3 digit number ` `        ``LinkedHashMap hm ` `            ``= ``new` `LinkedHashMap<>(); ` ` `  `        ``// first three digit number ` `        ``val = (s.charAt(``0``) - ``'0'``) * ``100` `              ``+ (s.charAt(``1``) - ``'0'``) * ``10` `              ``+ (s.charAt(``2``) - ``'0'``); ` ` `  `        ``hm.put(val, ``1``); ` `        ``for` `(i = ``3``; i < s.length(); i++) { ` `            ``val = (val % ``100``) * ``10` `+ s.charAt(i) - ``'0'``; ` ` `  `            ``// if key already exists ` `            ``// increase value by 1 ` `            ``if` `(hm.containsKey(val)) { ` `                ``hm.put(val, hm.get(val) + ``1``); ` `            ``} ` `            ``else` `{ ` `                ``hm.put(val, ``1``); ` `            ``} ` `        ``} ` ` `  `        ``// Output the three digit numbers with frequency>1 ` `        ``for` `(Map.Entry en : hm.entrySet()) { ` `            ``int` `key = en.getKey(); ` `            ``int` `value = en.getValue(); ` `            ``if` `(value > ``1``) ` `                ``System.out.println(key + ``" - "` `+ value + ``" times"``); ` `        ``} ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` ` `  `        ``// Input string ` `        ``String input = ``"123412345123456"``; ` ` `  `        ``// Calling Function ` `        ``printNum(input); ` `    ``} ` `} `

## C#

 `// C# program to print 3 digit repeating numbers ` `using` `System; ` `using` `System.Collections.Generic;              ` ` `  `class` `GFG ` `{ ` ` `  `    ``// function to print 3 ` `    ``// digit repeating numbers ` `    ``static` `void` `printNum(String s) ` `    ``{ ` `        ``int` `i = 0, val = 0; ` ` `  `        ``// Hashmap to store the ` `        ``// frequency of a 3 digit number ` `        ``Dictionary<``int``,  ` `                   ``int``> hm = ``new` `Dictionary<``int``,  ` `                                            ``int``>(); ` ` `  `        ``// first three digit number ` `        ``val = (s[0] - ``'0'``) * 100 +  ` `              ``(s[1] - ``'0'``) * 10 +  ` `              ``(s[2] - ``'0'``); ` ` `  `        ``hm.Add(val, 1); ` `        ``for` `(i = 3; i < s.Length; i++)  ` `        ``{ ` `            ``val = (val % 100) * 10 + s[i] - ``'0'``; ` ` `  `            ``// if key already exists ` `            ``// increase value by 1 ` `            ``if` `(hm.ContainsKey(val))  ` `            ``{ ` `                ``hm[val] = hm[val] + 1; ` `            ``} ` `            ``else`  `            ``{ ` `                ``hm.Add(val, 1); ` `            ``} ` `        ``} ` ` `  `        ``// Output the three digit numbers with frequency>1 ` `        ``foreach``(KeyValuePair<``int``, ``int``> en ``in` `hm)  ` `        ``{ ` `            ``int` `key = en.Key; ` `            ``int` `value = en.Value; ` `            ``if` `(value > 1) ` `                ``Console.WriteLine(key + ``" - "` `+  ` `                                  ``value + ``" times"``); ` `        ``} ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main(String []args) ` `    ``{ ` ` `  `        ``// Input string ` `        ``String input = ``"123412345123456"``; ` ` `  `        ``// Calling Function ` `        ``printNum(input); ` `    ``} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```123 - 3 times
234 - 3 times
345 - 2 times
```

My Personal Notes arrow_drop_up

Enthusiastic Coder Developer and always a learner

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 :

Be the First to upvote.

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