Java Program to Print All the Repeated Numbers with Frequency in an Array Last Updated : 27 Nov, 2020 Comments Improve Suggest changes Like Article Like Report The frequency of an element in an array is the count of the occurrence of that particular element in the whole array. Given an array that may contain duplicates, print all repeated/duplicate elements and their frequencies. Below is the discussion of this program by two approaches: Using a counter array: By maintaining a separate array to maintain the count of each element.Using HashMap: By updating the count of each array element in the HashMap<Integer, Integer>. Example : Input : arr[] = {1, 2, 2, 1, 1, 2, 5, 2} Output : 1 3 2 4 // here we will not print 5 as it is not repeated Input : arr[] = {1, 2, 3} Output : NULL // output will be NULL as no element is repeated.Method 1: (using counter array) Explanation : The array can be sorted as well as unsorted.First, count all the numbers in the array by using another array.A be an array, A[ ] = {1, 6 ,4 ,6, 4, 8, 2, 4, 1, 1}B be a Counter array B[x] = {0}, where x = max in array A "for above example 8".In a for loop, initialized with i. Increase value in counter array for every element in array A.B [ A [i] ]++; this will give counter array asArray B after counting elementsFinally print the index of B with its element whenever the element is greater than 1. Java // Java program to count the frequency of // elements in an array by using an extra // counter array of size equal to the maximum // element of an array import java.io.*; class GFG { public static void main(String[] args) { int A[] = { 1, 6, 4, 6, 4, 8, 2, 4, 1, 1 }; int max = Integer.MIN_VALUE; for (int i = 0; i < A.length; i++) { if (A[i] > max) max = A[i]; } int B[] = new int[max + 1]; for (int i = 0; i < A.length; i++) { // increment in array B for every integer // in A. B[A[i]]++; } for (int i = 0; i <= max; i++) { // output only if element is more than // 1 time in array A. if (B[i] > 1) System.out.println(i + "-" + B[i]); } } } Output: 1-3 4-3 6-2 The above approach can consume more space which is equal to the maximum constraint of the element of array Time Complexity: O(n) Space Complexity: O(K) where K is the maximum element/constraint of an array Method 2: (Using HashMap)It is a simple method and has a base logic, you can do it by using an unordered Map as well.The map will save a lot of space and time. Java // Java program to maintain // the count of each element // by using map. import java.util.*; class GFG { public static void main(String[] args) { int[] a = { 1, 6, 4, 6, 4, 8, 2, 4, 1, 1 }; int n = a.length; // size of array HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { if (map.containsKey(a[i])) { // if element is already in map // then increase the value of element at // index by 1 int c = map.get(a[i]); map.replace(a[i], c + 1); } // if element is not in map than assign it by 1. else map.put(a[i], 1); } for (Map.Entry<Integer, Integer> i : map.entrySet()) { // print only if count of element is greater // than 1. if (i.getValue() > 1) System.out.println(i.getKey() + " " + i.getValue()); else continue; } } } Output1 3 4 3 6 2 Time Complexity: O(n) Space Complexity: O(n) Comment More infoAdvertise with us Next Article Java Program to Print All the Repeated Numbers with Frequency in an Array kushwahp1234 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-Array-Programs +1 More Practice Tags : Java Similar Reads Java Program to Print the kth Element in the Array We need to print the element at the kth position in the given array. So we start the program by taking input from the user about the size of an array and then all the elements of that array. Now by entering the position k at which you want to print the element from the array, the program will print 2 min read Java Program to Find the Most Repeated Word in a Text File Map and Map.Entry interface will be used as the Map interface maps unique keys to values. A key is an object that is used to retrieve a value at a later date. The Map.Entry interface enables you to work with a map entry. Also, we will use the HashMap class to store items in "key/valueâ pairs and acc 3 min read Java Program to Print the Elements of an Array An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It's important to note that arrays in Java function differently than they do in C/C++As you see, the array of size 9 holds elem 6 min read Java program to print all duplicate characters in a string Given a string, the task is to write Java program to print all the duplicate characters with their frequency Example: Input: str = "geeksforgeeks" Output: s : 2 e : 4 g : 2 k : 2 Input: str = "java" Output: a : 2 Approach: The idea is to do hashing using HashMap. Create a hashMap of type {char, int} 2 min read Remove all Occurrences of an Element from Array in Java In Java, removing all occurences of a given element from an array can be done using different approaches that are,Naive approach using array copyJava 8 StreamsUsing ArrayList.removeAll()Using List.removeIf()Problem Stament: Given an array and a key, the task is to remove all occurrences of the speci 5 min read Java program to count the occurrence of each character in a string using Hashmap Given a string, the task is to write a program in Java which prints the number of occurrences of each character in a string. Examples: Input: str = "GeeksForGeeks" Output: r 1 s 2 e 4 F 1 G 2 k 2 o 1 Input: str = "Ajit" Output: A 1 t 1 i 1 j 1 An approach using frequency[] array has already been dis 2 min read Java Program for Last duplicate element in a sorted array We have a sorted array with duplicate elements and we have to find the index of last duplicate element and print index of it and also print the duplicate element. If no such element found print a message. Examples: Input : arr[] = {1, 5, 5, 6, 6, 7} Output : Last index: 4 Last duplicate item: 6 Inpu 2 min read Program to print numbers with diamond pattern write a program where each column represents same number according to given example:Examples : Input : 5 Output : 1 212 32123 212 1 Input : 7 Output : 1 212 32123 4321234 32123 212 1 C++ // C++ program to print diamond pattern #include<iostream> using namespace std; void display(int n) { // sp 6 min read Java Program to Remove Duplicate Elements From the Array Given an array, the task is to remove the duplicate elements from an array. The simplest method to remove duplicates from an array is using a Set, which automatically eliminates duplicates. This method can be used even if the array is not sorted.Example:Java// Java Program to Remove Duplicate // Ele 6 min read String Class repeat() Method in Java with Examples The string can be repeated N number of times, and we can generate a new string that has repetitions. repeat() method is used to return String whose value is the concatenation of given String repeated count times. If the string is empty or the count is zero then the empty string is returned. Syntax: 1 min read Like