# sequence number problem

• 10-08-2011, 02:19 PM
boys21
sequence number problem
hi,

I need to write a program for following data.

consider a sequence of numbers

10,1,1,11,1,12,13,14,1,1,15,1,16,1,1,1,1,1,1,17,18 ,1,19,1,1,1,1,20 and so on

i need to remove redundant values,and replace them with single value.. like ones in the above case, one is redundant and occured more than one time.

If it is single 1, it shouldnot be modified, if there are more that single 1's they need to be replaced by only one 1.

so, output should be 10,1,11,12,13,14,15,1,16,1,17,18,1,19,1,20.

can anyone give suggestions to implement this.

boys21
• 10-08-2011, 02:55 PM
JosAH
Re: sequence number problem
Use a Set<Integer> and check each number from the array if it is in the set already; if it is, it is a 'redundant' number, otherwise it is unique so far and you can stick it in the Set.

kind regards,

Jos
• 10-08-2011, 03:27 PM
eRaaaa
Re: sequence number problem
Quote:

Originally Posted by JosAH
Use a Set<Integer> and check each number from the array if it is in the set already; if it is, it is a 'redundant' number, otherwise it is unique so far and you can stick it in the Set.

kind regards,

Jos

?? Then you would add the "1" by example only once? But thats not what the TO want, or did I misunderstand you?

I would iterate over the array and add only these elements to the new array/list, which are not equal to the prvious one.

Quote:

Originally Posted by boys21
so, output should be 10,1,11,12,13,14,15,1,16,1,17,18,1,19,1,20.

11,12 ? not 11,1,12 ?
• 10-08-2011, 03:49 PM
JosAH
Re: sequence number problem
Ah, ok, I misread your problem, sorry for that; indeed you only add an element to the array if it isn't equal to the last element added to the same array.

kind regards,

Jos
• 10-08-2011, 04:45 PM
boys21
Re: sequence number problem
Quote:

Originally Posted by eRaaaa
?? Then you would add the "1" by example only once? But thats not what the TO want, or did I misunderstand you?

I would iterate over the array and add only these elements to the new array/list, which are not equal to the prvious one.

11,12 ? not 11,1,12 ?

sorry .. its 10,1 11,1,12,13,14,1,15.... and continues..

and thank you very much..i got the logic..
• 10-08-2011, 04:47 PM
boys21
Re: sequence number problem
thank you all very much for reply.. ill give a try.. and if any problem persits.. ill come back..

thanks again

/boys21
• 11-11-2011, 07:55 PM
boys21
Re: sequence number problem
hi,

I had a problem while writing the code, i was unable to capture all the numbers, few numbers are missing. Only thing i could get is no redundant one's.
code is like this.

let the array that contain all the numbers be initial array and new array created for storing the result is result array.

Code:

```result[0]=initial[0]; int a=1; int b=1;' while (true) {     if(inital[a]!=result[b-1])     {         result[b]=initial[a];         a++;         b++;     }     a++;     if(a>inital.length)         break; }```
print all values of result here!

can some one help me where am i going wrong?

/boys21
• 11-11-2011, 08:21 PM
boys21
Re: sequence number problem
i guess it should be while, instead of IF ?? then it might work ;)

thanks
boys21
• 11-12-2011, 02:55 AM
pbrockway2
Re: sequence number problem
Quote:

few numbers are missing
You are incrementing a twice when you find a match.
• 11-12-2011, 03:01 AM
pbrockway2
Re: sequence number problem
When you post code, use the "code" tags. You put [code] at the start of the code and [/code] at the end: that way the code will be corretcly formatted when it appears as part of a web page.

-----

A couple of style points which you might like to consider: First, I think something like resNdx and initNdx are more descriptive than a and b which are easily confused. Secondly, use braces even for single line if statements. Also, since you break out of the while loop with a check at the very end, you might as well use a do/while loop.