
Hi
i have array like {1,2,3,4,,5,5,6,6} then how can i display 1 time , 5 - 2times, 3- 2times
thank
kalins naik

Here is an example that store some integers into an array and find the occurrence of each number from the array.
import java.util.*;
public class SearchNumberOccurrence {
public static void searchAndDisplay(int[] arr) {
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
for (int element : arr) {
int index = list1.indexOf(element);
if (index != -1) {
int newCount = list2.get(index) + 1;
list2.set(index, newCount);
} else {
list1.add(element);
list2.add(1);
}
}
for (int i = 0; i < list1.size(); i++) {
System.out.println("Number " + list1.get(i) + " occurs "+ list2.get(i) + " times");
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] arr = {1,2,3,4,3,5,5,6,6};
searchAndDisplay(arr);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.