Results 1 to 10 of 10
  1. #1
    boys21 is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: sequence number problem

    Quote Originally Posted by JosAH View Post
    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 ?
    Last edited by eRaaaa; 10-08-2011 at 03:49 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    boys21 is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default Re: sequence number problem

    Quote Originally Posted by eRaaaa View Post
    ?? 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. #6
    boys21 is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default 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. #7
    boys21 is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default 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?

    thanks in advance

    /boys21
    Last edited by pbrockway2; 11-12-2011 at 01:56 AM. Reason: code tags and indents added

  8. #8
    boys21 is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default Re: sequence number problem

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

    thanks
    boys21

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: sequence number problem

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

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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

  1. number problem
    By click66 in forum New To Java
    Replies: 7
    Last Post: 10-11-2010, 03:27 PM
  2. Linked list sequence and array sequence
    By Predz in forum New To Java
    Replies: 1
    Last Post: 12-31-2009, 01:30 AM
  3. [B]Tab Sequence problem with radio buttons[/B]
    By shobha2k8 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-25-2008, 07:25 AM
  4. Replies: 0
    Last Post: 06-28-2008, 04:54 AM
  5. Escape sequence problem
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-21-2008, 10:15 PM

Posting Permissions

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