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; } }
19 ответов
+ 1
for (I=0; I<len; I++)
{
if (arr [I]==max){
return 1;
break;
}
else return 0;
+ 1
here it is:
arrange according to your need
https://code.sololearn.com/cGj6Rh8DL4hp/?ref=app
+ 1
if wrote it for if max is more than once, you can modify if you want just for twice.
0
you got max, now run a loop to check if there is another max in array, if it is return 1 else 0
0
thx Raj
0
I've entered the code it sez reached end of file while parsing
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
0
share code link here
0
I've shared two minutes ago
0
make it public. I cant find it.
copy code link and paste it here.
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
0
I've just posted
0
have you seen it Raj
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
0
OK thanks let's see what others say
0
one error is : you have missed ' }' at the end.
0
OK lemme put it Raj
0
thanks Raj lemme tweak it a lil bit but it runs
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;
}
}