Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Your input with written code (Sorting and searching within array)

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Your input with written code (Sorting and searching within array)

    So here's the assignment I was given:
    Writing a method that receives an int array titled values.
    the method has to return True, if throughout the array, there's an int that exists only once, or return false otherwise.
    Example : for an array 3,1,4,1,4,1 the method returns True
    for an array 3,1,4,1,4,3 method returns false.
    The received array is not sorted

    Here's my code


    public boolean single(int[] values) {
    boolean existsOnce = false;
     
    if(values.length == 1)
    return existsOnce = true;
    quicksort(values,0,(values.length -1));
     
    int counter = 0;
    int tempNumber = values[0];
    for(int i =0; i < values.length; )
    {
    if(tempNumber == values[i]){
    i++;
    counter ++;
    }
    else{
    if(counter == 1 || i == (values.length -1) )
    return existsOnce = true;
    else
    {
    tempNumber = values[i];
    counter= 0;
    }
    }
     
    }
    return existsOnce ;
    }

    Do you guys see any way of making this more efficient. also what would the "Big O" be? The class was about efficiency and that's what this method is supposed to strive for.


    Thanks!

    Gal
    Last edited by GalBenH; January 9th, 2012 at 02:18 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Your input with written code (Sorting and searching within array)

    Some words to the wise:

    Use indentation.
    Use brackets on every if and for, even if it's just one statement.
    Why: return existsonce = true;

    What do you think the "Big O" would be? Have you input different values of n to see how many iterations each takes?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Your input with written code (Sorting and searching within array)

    //Removed some - as to not void Kevin's post
    An easier solution might be to used the Arrays utility class.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Your input with written code (Sorting and searching within array)

    For what its worth, I do not see anywhere in the requirements that says the array should be sorted (yes, this is a hint in more ways than one)

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Your input with written code (Sorting and searching within array)

    Thanks all for your prompt help!
    Kevin,
    Regarding the existsOnce=true
    Before getting into any loop I wanted to eliminate the scenario of having an Array of only 1 int, which by deafult need this method to return True (for a single appearance of a specific int)


    newbie,
    I'm not supposed to use that as a part of the requirements.


    Copeg,

    That's an interesting approach, I was taking it for granted that a sorted array may make things simpler but it's definitely not a requirement.

    What do you think of this:


    public boolean single(int[] values) {
    boolean existsOnce = false;
    if(values.length == 1)
    return existsOnce = true;
     
    int counter = 0;
    int tempNumber = values[0];
    for(int i=0; i<values.length;i++){
      if(tempNumber == values[i]){
       counter ++;}
        else{
          if(counter == 1 || i == (values.length -1) )
          return existsOnce = true;
            else{
            tempNumber = values[i];
            counter= 0;}              
    }
    }
    return existsOnce ;
    }


    my main concern is that I'm not sure in this line:

    for(int i=0; i<values.length;i++){
    if(tempNumber == values[i]){
    counter ++;}

    If the tempNumber is progressed in ever step like the i is, in order to compare it the the remaining rest of the array in each step of the loop.

    I would love to see a sample correct solution from you guys (if possible) without making things too complicated (not supposed to be using advanced material).
    I actually learn a lot from these answers so please don't see it as a way of me trying to find an easy way out.
    My mind is drawing blank right now

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Your input with written code (Sorting and searching within array)

    counter ++;}
    Another comment on coding standards.
    Do NOT hide } on the same line with a statement.
    The ending } should be on a line by itself and in the same column as the statement line with the beginning {. That allows your eye to easily see the code structure and scope of variables.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Your input with written code (Sorting and searching within array)

    Quote Originally Posted by GalBenH View Post
    Kevin,
    Regarding the existsOnce=true
    Before getting into any loop I wanted to eliminate the scenario of having an Array of only 1 int, which by deafult need this method to return True (for a single appearance of a specific int)
    So why not just return true?

    Quote Originally Posted by GalBenH View Post
    What do you think of this:


    public boolean single(int[] values) {
    boolean existsOnce = false;
    if(values.length == 1)
    return existsOnce = true;
     
    int counter = 0;
    int tempNumber = values[0];
    for(int i=0; i<values.length;i++){
      if(tempNumber == values[i]){
       counter ++;}
        else{
          if(counter == 1 || i == (values.length -1) )
          return existsOnce = true;
            else{
            tempNumber = values[i];
            counter= 0;}              
    }
    }
    return existsOnce ;
    }
    I'm honestly not really sure how that's supposed to solve your problem. Does it?


    Quote Originally Posted by GalBenH View Post
    I would love to see a sample correct solution from you guys (if possible) without making things too complicated (not supposed to be using advanced material).
    I actually learn a lot from these answers so please don't see it as a way of me trying to find an easy way out.
    My mind is drawing blank right now
    That's not really how this works. Does your solution work? If not, step through it with a debugger to see exactly what it's doing. Think about your approach- how would you do this in your head, without a computer? Really examine what you're actually doing in your head instead of saying "well I would just see if there were two numbers that are the same." How would you really do that? What if the list was a thousand numbers? How would you do it in your head or with a piece of paper and a pencil then?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Your input with written code (Sorting and searching within array)

    I think I finally got it:



    public boolean single(int[] values)
    {
     
        if (values.length == 1)
        {
            return true;
        }
     
     
        for (int i=0; i<values.length; i++)
        {
            int counter = 0;
            int tempNumber = values[i];
     
            for (int j=0; j<values.length; j++)
            {
                if(tempNumber == values[j])
                {
                    counter++;
                }
     
                if (j == values.length && counter == 1)
                {
                    return true;
     
                }
                else {
                    return false
                }
            }
        }   
    }

    Do you guys approve?

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Your input with written code (Sorting and searching within array)

    Like I keep hinting at, it doesn't matter whether we approve. Does it do what you want? Do you understand how it works? Then don't worry about it.

    Actually, I don't see how this can possibly do what you described. Did you try running it with different inputs?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Your input with written code (Sorting and searching within array)

    Can you explain the logic for your code?
    For example:
    Why the counter?
    When in the search do you know that the condition you are looking for is true?

Similar Threads

  1. Sorting and Searching: Most Efficient Code
    By nicsa in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 29th, 2011, 11:20 PM
  2. My AP CS Book Has Code Written in JRE5
    By RAWBERRY in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 24th, 2011, 07:55 PM
  3. I NEED A WRITTEN CODE
    By samy222 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 22nd, 2010, 05:28 PM
  4. problem searching for a String element in a parallel array
    By david185000 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2010, 01:24 PM
  5. Having trouble insert/sorting array values w/ binary searching.
    By bh-chobo in forum Collections and Generics
    Replies: 4
    Last Post: October 8th, 2009, 02:38 AM