Need help in the below mentioned code | Selenium Forum
K
Karan Vaidyanathan Atreya Posted on 11/02/2021

Q.7 Print how many times has a number repeated in following array
int x[] ={1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43,2,4,7,7,5,2,1,3,4,6,311,1};

Can you please help out in explaining this code.

You can debug the program in eclipse to understand the flow and logic
Basically we will arrange the array in asending order.
public class Ques10 {
public static void main(String[] args) {
int x[] = { 1, 3, 4, 5, 6, 3, 2, 4, 6, 7, 9, 4, 12, 3, 4, 6, 8, 9, 7, 6, 43, 2, 4, 7, 7, 5, 2, 1, 3, 4, 6, 311,1 };
// arrange in ascending order
for (int i = 0; i < x.length - 1; i++) {
for (int j = i; j < x.length; j++) {

Why is the value incremented from 0 to length -1, in this case it will go from 0 - 31.
It should be i< x.length right?

Can you please help out.

Thanks


A
Ashish Thakur Replied on 12/02/2021

Arrange the array in ascending order and then do it :)


K
Karan Vaidyanathan Atreya Replied on 12/02/2021

Thanks will try the same.