1. Member
Join Date
May 2010
Posts
14
Rep Power
0

## 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

2. ## 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

3. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
8

## Re: sequence number problem

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.

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 ?
Last edited by eRaaaa; 10-08-2011 at 03:49 PM.

4. ## 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

5. Member
Join Date
May 2010
Posts
14
Rep Power
0

## Re: sequence number problem

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..

6. Member
Join Date
May 2010
Posts
14
Rep Power
0

## 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

7. Member
Join Date
May 2010
Posts
14
Rep Power
0

## 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.

Java 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
Last edited by pbrockway2; 11-12-2011 at 02:56 AM. Reason: code tags and indents added

8. Member
Join Date
May 2010
Posts
14
Rep Power
0

## Re: sequence number problem

i guess it should be while, instead of IF ?? then it might work ;)

thanks
boys21

9. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## Re: sequence number problem

few numbers are missing
You are incrementing a twice when you find a match.

10. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•