how to check and print the no of times an integer is repeated in an array

how to check and print the no of times an integer is repeated in an array

how to check and print the no of times an integer is repeated in an array???

suppose my input is 2 4 2 5 2 4 3 4 5 2 then my output should be

2-4 times 4-3 times 5-2 times 3-1 times

View Answers

January 7, 2011 at 12:07 PM

Hi Friend,

Try the following code:

import java.util.*;

class  CountNumberOccurrence {
  public static void main(String[] args) throws Exception{
  int array[]={2,4,2,5,2,4,3,4,5,2};
  for(int counter =0;counter<array.length;counter++){
  int n= array[counter];
  int count=0;
  for ( int i=0; i<array.length; i++){
  if (n==array[i])
  count++;
}
boolean flag=false;
for(int j=counter-1;j>=0;j--){
if(n==array[j])
flag=true;
}
if(!flag){
System.out.println(n+" - "+count+" times ");
}
}
}
}

Thanks

Ads









Related Tutorials/Questions & Answers:
how to check and print the no of times an integer is repeated in an array
how to check and print the no of times an integer is repeated in an array  how to check and print the no of times an integer is repeated in an array??? suppose my input is 2 4 2 5 2 4 3 4 5 2 then my output should be 2-4
How can print integer in method
How can print integer in method   a method call process public process(int jobid,int remain){ } how to print int jobid ,int remain in for loop jobid in proc[0],remain in proc[1],both show in text file how to print proc[0
Advertisements
How to Print Array in JavaScript
How to Print Array in JavaScript In this section you will learn about Array in JavaScript. Array are important part of all programming languages. Array...;<h2>Click to print the array element</h2></p><p><input
Java Reverse integer array
Java Reverse integer array In this tutorial, you will learn how to reverse integer array. Here, we have created a method to reverse an array which has been passed as a parameter and returns a new array with all the elements in reverse
Reverse integer array program
Reverse integer array program  Been tasked with the following question: Write a method that accepts an integer array and returns a new array with all the elements in reverse order. For example, if the input array is [2, 4, 6, 8
how i can print the elements before a spicific value in array queue?
how i can print the elements before a spicific value in array queue?  how i can printall elements befor the spicific value and print this value also   Hi Friend, Try this: <html> <script> var arr=new Array
How to put text file numbers to array and check the position of numbers?
How to put text file numbers to array and check the position of numbers?   I have numbers in text file data.txt 12 9 8 3 1
ModuleNotFoundError: No module named 'print_array'
'print_array' How to remove the ModuleNotFoundError: No module named 'print_array' error? Thanks   Hi, In your python environment...ModuleNotFoundError: No module named 'print_array'  Hi, My Python
How to Remove Repeated Characters from the String?
How to Remove Repeated Characters from the String?  Hi, I trying to develop an application which will remove the repeated characters from... the example of how to remove repeated characters from the string. Please visit
Simplest way to print an array in Java
Simplest way to print an array in Java  Simplest way to print an array in Java
print array in following format
print array in following format  i want print two array in following format first array= 1,2,3,4,5,6,7,8,9 second array=11,12,13,14,15,16,17,18,19 i...; Here is a code that displays the array values in the following pattern: 1 2
How to declare array in Java?
of declarating integer array: int[] nums = new String[]{1,2, 5, 7}; Check...How to declare array in Java?  Hi, How to declare array in Java? Thanks   Hi, Following is a code of declaration of String array
how to print the average of each column of 2d n*m array using java
how to print the average of each column of 2d n*m array using java  here is my code: import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public
How to Prepend Array in PHP
How to Prepend Array in PHP  hi, How can i prepand array in PHP...;The original array is:</b><br/>"); print_r($array);array_unshift($array... at the beginning:</b><br/>"); print_r($array); ?> Thanks
How to calculate average of array in Java?
How to calculate average of array in Java?  Hi, I have an integer array and I want to calculate the average of the values in it. How to calculate... of the array. This way you can achieve the result. Check source code at Average
JavaScript array print
JavaScript array print     ... print them wherever we wish. Here is the simple example of array printing which... have used a for loop to print all the elements of the array. for(var i
how to print the server time
how to print the server time  how to print the server time in jsp and update time in fix interval
how to print dot in python without newline
how to print dot in python without newline  Hi, I a debugging... to print 20 dots without new line. Is there any easy solution? how to print dot...); .......... You can also define a variable to print the no of times
how to print dot in python without newline
how to print dot in python without newline  Hi, I a debugging... to print 20 dots without new line. Is there any easy solution? how to print dot...); .......... You can also define a variable to print the no of times
print selected checkbox names in array without form tag
print selected checkbox names in array without form tag  Hi everyone ... I have problem in my program. I have hashmap i.e. collection , my code is as follows [code] <%! private HashMap<Integer, Domain>domainmap
How to Convert ArrayList to Array?
How to Convert ArrayList to Array?  Hi, I am trying to learn to Convert ArrayList to Array. How it is possible? How to Convert ArrayList to Array... ArrayList to Array. Check more examples at Convert ArrayList to Array. Thanks
How to change object item to integer item
How to change object item to integer item  How to change objectitem to integer item?thankyou
Create array of random size
Create array of random size In this tutorial, you will learn how to create a randomly sized integer array. For this, we have declared an array and using the Random class, we have set the random size to array. We have stored
How to print this Format exactly?
How to print this Format exactly?   * * * & reverse
how to check gremlin version
how to check gremlin version  Hi, I am inside gremlin console and want to check the version. How to check gremlin version? thanks   Hi...; Gremlin.version() ==>3.3.1 this will print the gremlin version. Thanks
how to create array in python
how to create array in python  Hi, how to create array in python? I want to create an array in Python and add some values to it. Thanks   Hi, You can create a new array with the code myArray = [] and after
How to: generic array creation
How to: generic array creation  How to: generic array creation
ModuleNotFoundError: No module named 'odoo8-addon-res-currency-print-on-check'
: ModuleNotFoundError: No module named 'odoo8-addon-res-currency-print-on-check' How...ModuleNotFoundError: No module named 'odoo8-addon-res-currency-print-on-check...-print-on-check' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'odoo8-addon-res-currency-print-on-check'
: ModuleNotFoundError: No module named 'odoo8-addon-res-currency-print-on-check' How...ModuleNotFoundError: No module named 'odoo8-addon-res-currency-print-on-check...-print-on-check' error? Thanks   Hi, In your python
How does Java handle integer overflows and underflows?
How does Java handle integer overflows and underflows?   hi, How does Java handle integer overflows and underflows? Thanks
How to print this in java?
How to print pattern in Java?  How to print a particular pattern in Java...;  How to print this in java

Ads