Logic to find the repeated number is not working correctly | Selenium Forum
M
Manisha Gupta Posted on 17/04/2019

int repeat[] ={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};

for (int i=0;i<repeat.length;i++)
{
for(int j=1;j<repeat.length;j++)
{
if(repeat[i]>repeat[j])
{
int temp= repeat[i];
repeat[i]=repeat[j];
repeat[j]=temp;
}
}
}

for(int i=0; i<repeat.length;i++)
{
System.out.print(repeat[i] + " ");
}

int counter=1;
for(int i=0; i<repeat.length-1;i++)
{
if(repeat[i] != repeat[i+1])
{
System.out.println(repeat[i] + "repeated" + counter + " Times");
counter=1;
}else {
counter++;

}

}

 

 

 

Output as below , which is not correct as its missing the correct count for "1"

 

1 repeated1 Times
311repeated1 Times
43repeated1 Times
12repeated1 Times
9repeated2 Times
8repeated1 Times
7repeated4 Times
6repeated5 Times
5repeated2 Times
4repeated6 Times
3repeated4 Times
2repeated3 Times


A
Ashish Thakur Replied on 25/04/2019

Arrange the array in ascending order first and then it will help you do it