# Java Program to Find the Frequency of All Duplicate Elements in an Array

«
»

This is the Java Program to Find Repeated Elements and the Frequency of Repetition.

Problem Description

Given an array of integers, find and print the repeated elements and their frequency of repetition.

Example:
Array: [1,2,3,4,5,5,3]

Output:
Element—–>Frequency

3—–>2
5—–>2

Problem Solution
Note: Join free Sanfoundry classes at Telegram or Youtube

Sort the array, and find the frequency of each repeated element and print it.

Program/Source Code

Here is the source code of the Java Program to Find Repeated Elements and the Frequency of Repetition. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

1. ` `
2. `// Java Program to Find Repeated Elements and the Frequency of Repetition.`
3. ` `
4. `import java.io.BufferedReader;`
5. `import java.io.InputStreamReader;`
6. `import java.util.Arrays;`
7. ` `
8. `public class RepeatedElementsFrequency {`
9. `    // Function to display the repeated elements and their frequencies `
10. `    static void displayOutput(int[] array){`
11. `        int i,j,frequency;`
12. `        for(i=0; i<array.length; i++){`
13. `            frequency = 1;`
14. `            for(j=i+1; j<array.length; j++){`
15. `                if(array[j] == array[i]){`
16. `                    frequency++;`
17. `                }`
18. `                else{`
19. `                    break;`
20. `                }`
21. `            }`
22. `            i=j-1;`
23. `            if(frequency > 1){`
24. `                System.out.println("The element is " + array[i] `
25. `                                   + " and its frequency is " + frequency);`
26. `            }`
27. `        }`
28. `    }`
29. `    // Function to read input`
30. `    public static void main(String[] args){`
31. `        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));`
32. `        int size;`
33. `        System.out.println("Enter the size of the array");`
34. `        try {`
35. `            size = Integer.parseInt(br.readLine());`
36. `        } catch (Exception e) {`
37. `            System.out.println("Invalid Input");`
38. `            return;`
39. `        }`
40. `        int[] array = new int[size];`
41. `        System.out.println("Enter array elements");`
42. `        int i;`
43. `        for (i = 0; i < array.length; i++) {`
44. `            try {`
45. `                array[i] = Integer.parseInt(br.readLine());`
46. `            } catch (Exception e) {`
47. `                System.out.println("An error occurred");`
48. `                return;`
49. `            }`
50. `        }`
51. `        Arrays.sort(array);`
52. `        displayOutput(array);`
53. `    }`
54. `}`
Program Explanation

1. In function displayOutput(), a variable frequency is created.
2. The loop for(i=0; i<array.length; i++) is used to iterate through the array and it initializes frequency to 1 in each iteration.
3. The nested loop for(j=i+1; i<array.length; j++) traverses the remaining part of the array and count the elements frequency.
4. If the frequency is greater than 1, than the element is displayed along with frequency.

Take Java Programming Practice Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Time Complexity: O(n*log(n)) where n is the number of elements in the array.

Runtime Test Cases
```

Case 1 (Simple Test Case):

Enter the size of the array
7
Enter array elements
1
2
3
4
5
5
3
The element is 3 and its frequency is 2
The element is 5 and its frequency is 2

Case 2 (Simple Test Case - another example):

Enter the size of the array
5
Enter array elements
1
1
1
1
1
The element is 1 and its frequency is 5```

Sanfoundry Global Education & Learning Series – Java Programs. 