0

Guys what if I want my code to return 1 if in my array the largest number is repeated twice and to return 0 if not?how

public class ArrayList { public static void main(String[] args) { int [] g = {9,2,4,5,9}; int max = getMax(g) ; System.out.println(max); } private static int getMax(int [] nums) { int max = nums[0]; for (int i = 1; i < nums.length; i++){ if (nums[i] > max) { max = nums[i]; } } return max; } }

27th Feb 2017, 7:07 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
19 Réponses
+ 1
for (I=0; I<len; I++) { if (arr [I]==max){ return 1; break; } else return 0;
27th Feb 2017, 7:39 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
+ 1
here it is: arrange according to your need https://code.sololearn.com/cGj6Rh8DL4hp/?ref=app
27th Feb 2017, 9:16 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
+ 1
if wrote it for if max is more than once, you can modify if you want just for twice.
27th Feb 2017, 9:23 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
you got max, now run a loop to check if there is another max in array, if it is return 1 else 0
27th Feb 2017, 7:37 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
thx Raj
27th Feb 2017, 7:48 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
0
I've entered the code it sez reached end of file while parsing
27th Feb 2017, 7:53 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
0
public static void main(String[] args) { double[] g = {1.9,2.9,3.4,3.5}; double max = getMax(g); System.out.println(max); } private static double getMax(double[] nums) { double max = nums[0]; for (int i = 1; i < nums.length; i++){ if (nums[i] > max) { max = nums[i]; } } return max; } for (i=0; i<nums.length; i++) { if (nums [i]==max){ return 1; break; } else return 0; } it's not running
27th Feb 2017, 7:56 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
0
share code link here
27th Feb 2017, 7:57 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
I've shared two minutes ago
27th Feb 2017, 7:58 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
0
make it public. I cant find it. copy code link and paste it here.
27th Feb 2017, 8:03 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
public class ArrayList { public static void main(String[] args) { double[] g = {1.9,2.9,3.4,3.5}; double max = getMax(g); System.out.println(max); } private static double getMax(double[] nums) { double max = nums[0]; for (int i = 1; i < nums.length; i++){ if (nums[i] > max) { max = nums[i]; } } return max; } for (i=0; i<nums.length; i++) { if (nums [i]==max){ return 1; break; } else return 0; } OK how about now
27th Feb 2017, 8:05 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
0
I've just posted
27th Feb 2017, 8:05 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
0
have you seen it Raj
27th Feb 2017, 8:14 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
0
man I dont know java but I feel it should be double g [ ] and double nums [ ]. I am trying to figure out what else is going wrong here. can write it in C, C++, Python but before that lets have some fight with java :p
27th Feb 2017, 8:43 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
OK thanks let's see what others say
27th Feb 2017, 8:44 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
0
one error is : you have missed ' }' at the end.
27th Feb 2017, 8:55 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
OK lemme put it Raj
27th Feb 2017, 8:57 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
0
thanks Raj lemme tweak it a lil bit but it runs
27th Feb 2017, 9:20 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
0
//on demannd // print 1 if max more than once else //print 0 public class ArrayList { public static void main(String[] args) { double[] g = {1.9,2.9,3.5,3.5}; double max = getMax(g); System.out.println(max); int len= g.length; int count=0; // System .out .println (len); for(int i=0; i<len;i++) { if(g[i]==max) { count=count+1; } } if(count>=2) System .out.println ("1"); else System .out.println ("0"); } private static double getMax(double[] nums) { double max = nums[0]; for (int i = 1; i < nums.length; i++){ if (nums[i] > max) { max = nums[i]; } } return max; } }
27th Feb 2017, 9:22 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar