Results 1 to 5 of 5
- 12-25-2011, 11:49 PM #1
- 12-26-2011, 12:32 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: May I know the algorithm to calculate how many different values are in the array?
To calculate how many distinct elements are in an array put the array elements into an instance of Set and look at its size. See the example in the The Set Interface page of Oracle's Tutorial.
To find out both the distinct elements *and* their frequencies you could use a Map which links each distinct element (as key) with its frequency (as value). For each element of the array you add an entry to the map or update an existing one: if the element is there already you increment the count, if not you create a new entry with a count of 1. Again, there is an example in the Tutorial's Collection on the page The Map Interface.
- 12-29-2011, 04:54 AM #3
Re: May I know the algorithm to calculate how many different values are in the array?
Thank you
- 12-29-2011, 05:16 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: May I know the algorithm to calculate how many different values are in the array?
You're welcome
- 03-21-2012, 08:27 PM #5
Map Interface- doubt
input
policy1= {N, Y, Y, Y, N, Y, N, NULL, Y,N}
I got the following outputJava Code:Map<String, Integer> m = new HashMap<String, Integer>(); for(j=0;j<=9;j++) { Integer freq = m.get(policy1[j]); m.put(policy1[j], (freq == null) ? 1 : freq + 1); } System.out.println(m.keySet()); System.out.println(m.values()); System.out.println(m.entrySet()); System.out.println(m.size() + " distinct words:"); System.out.println(m);
3 distinct words:
[null, N , Y ]
[1, 4, 5]
[null=1, N =4, Y =5]
3 distinct words:
{null=1, N =4, Y =5}
Thanking You,Further I want to assign
A1=null, A2=N, A3=Y
A1size=1, A2size=3, A3size=6
Highest value=A3
for(j=0;j<=9;j++){
If(policy1[j]==null)
If(A2>A3)
policy1[j]=A2;
else
policy1[j]=A3;
I Don’t know how can I utilize m values.{null=1, N =3, Y =6}
With Regards,
Nandhini.
Similar Threads
-
Code to calculate mode using array
By Nox_1031 in forum New To JavaReplies: 19Last Post: 09-06-2011, 05:48 PM -
I want to know how to calculate and display the sum of each column in 2D array
By Javanoobs in forum New To JavaReplies: 1Last Post: 03-03-2011, 06:43 AM -
How to write a program to calculate and display sum in two dimensional array?
By Javanoobs in forum New To JavaReplies: 12Last Post: 02-08-2011, 02:58 PM -
need to input values from a text file into an array and count values
By pds8475 in forum New To JavaReplies: 14Last Post: 01-22-2011, 02:36 PM -
How to Read data from text file and calculate the values?
By janeansley in forum New To JavaReplies: 40Last Post: 07-04-2008, 08:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks