Repeated Array | Selenium Forum
M
Posted on 21/02/2016
int s[]={1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,12,7,6,43,2,4,7,7,5,2,1,3,4,6,1};
for (int i=0;i<=s.length;i++) {
int count=1;
for(int j=i+1;j<s.length;j++)
{ if(s[i]==s[j])
count++;}
System.out.println(s[i]+ "-Repeated "+ count+" times");}

I tried to get repeated array only one time for each no. however getting it every time.
Please help me out in resolving this issue.

Thanks
Sonia Ojha

M
Replied on 21/02/2016

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


M
Replied on 24/02/2016

Hi,
I gone through your code however that doesn't work correctly if number is greater than 10.E.g. if we have 12 in our Array your logic is skipping number 12.Moreover, it giving output for number 0 which is not part of array s[]. In my below logic it works for any number but prints number every time it repeated e.g.
For Number 1, I am getting below output
1-Repeated 3 times
1-Repeated 2 times
I want it should print number 1 only once with output (1-Repeated 3 times) and skip number 1 every next time it repeated.

My Logic
public static void main(String []arg){

int s[]={1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,12,7,6,43,2,4,7,7,5,2,1,3,4,6,1};
for (int i=0;i<=s.length;i++) {
int count=1;
for(int j=i+1;j<s.length;j++)
{ if(s[i]==s[j])
count++;
}
if(count>1)
System.out.println(s[i]+ "-Repeated "+ count+" times");

}
}
}

Please have a look and provide me solution if possible.

Thanks
Sonia Ojha


M
Replied on 24/02/2016

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);
}
}
System.out.println(i + "- Repeated " + count + " times");
count = 0;
}
}


M
Replied on 26/02/2016

Hi,

Thanks for reply!
Still not working as expected.Number 0 is not part of array however output is ( 0- Repeated 1 times), number 12 is not repeated anywhere in the array though output is (12- Repeated 1 times).Moreover, if I have a number 100 part of our array,then we are getting output for all those numbers which are not the part of array.
I want an output which will print only repeated numbers in the array and they should be printed only 1 time.
for e.g for below array
int[] x = { 1, 3, 4, 12,100,100, 2, 7, 7,1, 1, 1 };
number 1 is coming 4 times in the array and repeated 3 times.

Output should be:
1- Repeated 3 times
7- Repeated 1 times
100- Repeated 1 times

Thanks
Sonia Ojha


M
Replied on 28/02/2016

[quote:3imaxz38]Number 0 is not part of array however output is ( 0- Repeated 1 times), number 12 is not repeated anywhere in the array though output is (12- Repeated 1 times).[/quote:3imaxz38]

you can make these superficial changes yourself.


[code:3imaxz38]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:3imaxz38]