Results 1 to 10 of 10
Thread: sequence number problem
- 10-08-2011, 02:19 PM #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.
Thanks in advance/
boys21
- 10-08-2011, 02:55 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-08-2011, 03:27 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: sequence number problem
?? 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 ?
Originally Posted by boys21
Last edited by eRaaaa; 10-08-2011 at 03:49 PM.
- 10-08-2011, 03:49 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-08-2011, 04:45 PM #5
Member
- Join Date
- May 2010
- Posts
- 14
- Rep Power
- 0
- 10-08-2011, 04:47 PM #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
- 11-11-2011, 06:55 PM #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.
print all values of result here!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; }
can some one help me where am i going wrong?
thanks in advance
/boys21Last edited by pbrockway2; 11-12-2011 at 01:56 AM. Reason: code tags and indents added
- 11-11-2011, 07:21 PM #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
- 11-12-2011, 01:55 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: sequence number problem
You are incrementing a twice when you find a match.few numbers are missing
- 11-12-2011, 02:01 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
Similar Threads
-
number problem
By click66 in forum New To JavaReplies: 7Last Post: 10-11-2010, 03:27 PM -
Linked list sequence and array sequence
By Predz in forum New To JavaReplies: 1Last Post: 12-31-2009, 01:30 AM -
[B]Tab Sequence problem with radio buttons[/B]
By shobha2k8 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 08-25-2008, 07:25 AM -
problem in parsing sequence diagram using emf
By Saniya in forum EclipseReplies: 0Last Post: 06-28-2008, 04:54 AM -
Escape sequence problem
By eva in forum New To JavaReplies: 2Last Post: 01-21-2008, 10:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks