Find the repetition of a number in an interger array
This program can be used to find the number of times a number repeats in a given array of the integers. In this program the size of the array is 10, you can change it to your requirements. This program is designed using the java language, using array of integers.
Description:
In the above program, a[] is the array which stores the user input and b[] is the flagging array which marks when the ith element of a is visited. The occu() is used to calculate the no.of times x repeats and marks the visited part of a[] in b[]. The calc() send x to occu when the ith element is not visited in a, so it checks the b[].
import java.util.Scanner; class counter { int a[]=new int[10],b[]=new int[10]; void getit() { Scanner s= new Scanner(System.in); System.out.println("Enter The Array Elements:"); for (int i=0;i<10;i++) { a[i]=s.nextInt(); b[i]=0; } } void occu(int x) { int c=0; for(int i=0;i<10;i++) { if(a[i]==x) { c=c+1; b[i]=1; } } System.out.println(x+" Occurs "+c+" times."); } void calc() { for(int i=0;i<10;i++) if(b[i]!=1) occu(a[i]); } } class mainDriver { public static void main(String []args) { counter s=new counter(); s.getit(); s.calc(); } }
Description:
In the above program, a[] is the array which stores the user input and b[] is the flagging array which marks when the ith element of a is visited. The occu() is used to calculate the no.of times x repeats and marks the visited part of a[] in b[]. The calc() send x to occu when the ith element is not visited in a, so it checks the b[].
Comments
Post a Comment