Question 10 in the excercise after video 2 | Selenium Forum
M
Posted on 07/09/2015
Question 10) from excercise is :

Suppose there is an integer array holding following elements:
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

Write a program which prints each number from array and the times it has been repeated in array
Fox e.g.
1- Repeated 3 times
4- Repeated 6 times


I tried the following code :

public static void repeatedarray(){
int [] h={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,3,1,1,1};
int [] k = h;
int c=0;
for(int a=0;a<h.length;a++){
for(int j=0; j<k.length;j++) {
if(h[a]==k[j]){
c++;
}
}
System.out.println(h[a]+"Repeated"+c+"times");
c=0;
}
}

Output that is printed by this program is :

1Repeated5times
3Repeated5times
4Repeated6times
5Repeated2times
6Repeated5times
3Repeated5times
2Repeated3times
4Repeated6times
6Repeated5times
7Repeated4times
9Repeated2times
4Repeated6times
12Repeated1times
3Repeated5times
4Repeated6times
6Repeated5times
8Repeated1times
9Repeated2times
7Repeated4times
6Repeated5times
43Repeated1times
2Repeated3times
4Repeated6times
7Repeated4times
7Repeated4times
5Repeated2times
2Repeated3times
1Repeated5times
3Repeated5times
4Repeated6times
6Repeated5times
3Repeated5times
1Repeated5times
1Repeated5times
1Repeated5times


Please suggest where am I going wrong.

Thanks
Sankalp

M
Replied on 08/09/2015

first of all try to sort the array.

you can different algorithm for them like bubble sort, quick sort etc.

after that is done make a program that takes in array and counts each recurrence and then print the results.


M
Replied on 08/09/2015

I tried sorting the array using the following code :
import java.util.Arrays;
public class Excercise() {
public static void main(){
int [] h={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,3,1,1,1};
int [] y=h;
Arrays.sort(y);
int c=0;
for(int i=0;i<y.length;i++){
for(int j=0;j<y.length;j++){
if(h[i]==y[j]){
c++;
}
}

System.out.println(y[i]+"Repeated"+c+"times");
c=0;
}
}
}

But now it is printing the following :

1Repeated5times
1Repeated5times
1Repeated5times
1Repeated5times
1Repeated5times
2Repeated3times
2Repeated3times
2Repeated3times
3Repeated5times
3Repeated5times
3Repeated5times
3Repeated5times
3Repeated5times
4Repeated6times
4Repeated6times
4Repeated6times
4Repeated6times
4Repeated6times
4Repeated6times
5Repeated2times
5Repeated2times
6Repeated5times
6Repeated5times
6Repeated5times
6Repeated5times
6Repeated5times
7Repeated4times
7Repeated4times
7Repeated4times
7Repeated4times
8Repeated1times
9Repeated2times
9Repeated2times
12Repeated1times
43Repeated1times

I think as per the question it should print only once e.g ."7Repeated4times" only once. Please suggest


M
Replied on 08/09/2015

mailed you solution


M
Replied on 11/09/2015

Hi Ashish,

I have tried with the following code

public class Module2_Exer9 {

public static void main(String[] args) {
// TODO Auto-generated method stub
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};
for ( int i=0; i<x.length; i++)
{
int count = 0;
for (int j= 0; j<x.length; j++)
{
if(x[i] == x[j])
{
count = count+1;
}

}
System.out.println("Value repeated"+ x[i] + " is " + count );

}

}

}


Getting the output as

Value repeated1 is 3
Value repeated3 is 4
Value repeated4 is 6
Value repeated5 is 2
Value repeated6 is 5
Value repeated3 is 4
Value repeated2 is 3
Value repeated4 is 6
Value repeated6 is 5
Value repeated7 is 4
Value repeated9 is 2
Value repeated4 is 6
Value repeated12 is 1
Value repeated3 is 4
Value repeated4 is 6
Value repeated6 is 5
Value repeated8 is 1
Value repeated9 is 2
Value repeated7 is 4
Value repeated6 is 5
Value repeated43 is 1
Value repeated2 is 3
Value repeated4 is 6
Value repeated7 is 4
Value repeated7 is 4
Value repeated5 is 2
Value repeated2 is 3
Value repeated1 is 3
Value repeated3 is 4
Value repeated4 is 6
Value repeated6 is 5
Value repeated311 is 1
Value repeated1 is 3


Also I have tried with sorting the array but still could not get the correct method
could you please suggest.

Thanks,
Chakradhar


M
Replied on 11/09/2015

Your code has very basic mistakes
I have mailed you solution as well
You need to sort it


M
Replied on 15/08/2016

10) Suppose there is an integer array holding following elements:
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

Write a program which prints which each number from array and the times it has been repeated in array
Fox eg
1- Repeated 3 times
4- Repeated 6 times

My code is not identifying the unique numbers. Please help

public class ExerciseModule2_10 {


public static void main(String[] args) {
// TODO Auto-generated method stub


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


int a = array.length;
int n=0;
System.out.println("Length of array is "+a);

int temp;
for (int i = 0; i < a; i++)
{
for (int j = i + 1; j < a; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}

for(int i=0;i<a;i++){
n=0;
for(int j =0;j<a;j++){

if(array[i]==array[j]){
n=n+1;
}

}

System.out.println("Number of " +array[i]+ " is " +n);
}

}

}


M
Replied on 15/08/2016

package com.soapuitutorial.propertie;

public class ArrayCount {

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, 4, 3, 2, 4, 7, 7, 5, 2, 1, 3, 4, 6, 3, 1, 1 };
int count = -1;
int biggestNumber = 0;

for (int i = 1; i < x.length; i++) {
if (x[i] > biggestNumber)
biggestNumber = x[i];
}
// System.out.print(x[i]+",");
for (int i = 0; i <= biggestNumber; i++) {
for (int j = 0; j <= x.length - 1; j++) {
if (x[j] == i) {
count++;
// System.out.println(x[i] + " " + i+" "+count);
}
}
if (count < 0) {
} else {
System.out.println(i + "- Repeated " + count + " times");
}
count = -1;
}
}

}


M
Replied on 02/09/2016

Hi
This is my code.
public class RepeatNo_Array {

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
};


int y=x.length;


for(int i=0;i<y;i++){
int count=0;

for(int j=i+1;j<y;j++){

if(x[i]==x[j]){

count=count+1;
System.out.println("integer" + x[i]+ "repeated" +count+ "times");
}

}




}


}

OUTPUT is
integer1repeated1times
integer1repeated2times
integer3repeated1times
integer3repeated2times
integer3repeated3times
integer4repeated1times
integer4repeated2times
integer4repeated3times
integer4repeated4times
integer4repeated5times
integer5repeated1times
integer6repeated1times
integer6repeated2times
integer6repeated3times
integer6repeated4times
integer3repeated1times
integer3repeated2times
integer2repeated1times
integer2repeated2times
integer4repeated1times
integer4repeated2times
integer4repeated3times
integer4repeated4times
integer6repeated1times
integer6repeated2times
integer6repeated3times
integer7repeated1times
integer7repeated2times
integer7repeated3times
integer9repeated1times
integer4repeated1times
integer4repeated2times
integer4repeated3times
integer3repeated1times
integer4repeated1times
integer4repeated2times
integer6repeated1times
integer6repeated2times
integer7repeated1times
integer7repeated2times
integer6repeated1times
integer2repeated1times
integer4repeated1times
integer7repeated1times
integer1repeated1times


Plz help me where i went wrong.
Regards
Sukanya


M
Replied on 02/09/2016

use debuging feature in your eclipse to learn how this code works.

[code:r00skcoo]package com.soapuitutorial.propertie;

public class ArrayCount {

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, 4, 3, 2, 4, 7, 7, 5, 2, 1, 3, 4, 6, 3, 1, 1 };
int count = -1;
int biggestNumber = 0;

for (int i = 1; i < x.length; i++) {
if (x[i] > biggestNumber)
biggestNumber = x[i];
}
// System.out.print(x[i]+",");
for (int i = 0; i <= biggestNumber; i++) {
for (int j = 0; j <= x.length - 1; j++) {
if (x[j] == i) {
count++;
// System.out.println(x[i] + " " + i+" "+count);
}
}
if (count < 0) {
} else {
System.out.println(i + "- Repeated " + count + " times");
}
count = -1;
}
}

}[/code:r00skcoo]