Results 1 to 11 of 11
  1. #1
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default Number of occurrences in a int[ ]

    I'm having a little trouble as to taking two int []'s and if there are two elements that are the same, skipping over them. In the end I need the program to print out the elements that don't have any duplicates at all. For instance:
    int [] a = {4, 5, 5}
    int [] b = {6, 6, 3}

    The program will output an array which contains,
    int [] output = {3, 4}

    Here's all the code I have
    import java.util.Arrays;
    Java Code:
    class Test2{
    public static void main(String args[]){
    int arr[] = new int[]{2, 5, 1, 1, 2, 3}; // 1, 1, 2, 2, 3, 5
    Arrays.sort(arr);
    //System.out.print(Arrays.toString(arr)) ;
     
    int newArray[] = new int[arr.length];
    int temp = 0 ;
    int count = 0 ;
     
    for(int i = 0; i < arr.length; i++){
    temp = arr[i];
    //System.out.print(temp + " ") ; // 1, 1, 2, 2, 3, 5
    int val = i + 1 ;
    //System.out.print(val + " ") ; // 1, 2, 3, 4, 5, 6
    if(val < arr.length){
    if(temp == arr[val])
    continue;
    }
    newArray[count++] = temp;
    //System.out.print(temp + " ") ; // 1, 2, 3, 5
    }
     
     
    for(int i = 0; i < newArray.length; i++){
    System.out.print(newArray[i] + " ");
    }
    }
    }

    The output = 1 2 3 5 0 0

    Please ignore all of the System.out.println's in the code. I was doing checks to try and narrow down the problem.


    Thank You I appreciate any input.

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    OK, I'm confused since your question concerns two arrays, but your code only has one. So, let me see if I understand the goal. Given two arrays, you want to return an array that contains only the elements that appear once.
    Now, it's not clear from your example if a value appeared once in each array if it should be included in your results, so I'm not sure how you want to handle that.

    Second, what is it you want from us? Your code already does what you need, or do you want to use a different method?

    To clarify, your code first sorts the array, then it compares adjacent members. If the next value is identical to the current one, you skip it. If the next value is different, you store the current value in the resulting array.

    Is your question how to extend this to two arrays?

    I'd love to help you, but could you ask a specific question instead of stating your problem and asking for help. It's much harder to answer a vague "I need help", then to answer "I need help with ...".
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by CodesAway View Post
    OK, I'm confused since your question concerns two arrays, but your code only has one. So, let me see if I understand the goal. Given two arrays, you want to return an array that contains only the elements that appear once.
    Now, it's not clear from your example if a value appeared once in each array if it should be included in your results, so I'm not sure how you want to handle that.

    Second, what is it you want from us? Your code already does what you need, or do you want to use a different method?

    To clarify, your code first sorts the array, then it compares adjacent members. If the next value is identical to the current one, you skip it. If the next value is different, you store the current value in the resulting array.

    Is your question how to extend this to two arrays?

    I'd love to help you, but could you ask a specific question instead of stating your problem and asking for help. It's much harder to answer a vague "I need help", then to answer "I need help with ...".
    Thanks for the prompt reply

    CodesAway: So, let me see if I understand the goal. Given two arrays, you want to return an array that contains only the elements that appear once.

    This is what I want the program to do.

    In the output it prints 1, 2, 3, 5 where it should only be outputting 3, 5 and thats it sorry for the misunderstanding

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Oh, ok, got it. Well, in your code, you only skip the value if the next one is the same. This means, if there are multiple such values, the last one is stored (since the next value is different).

    Instead, what you want to do is "if the next element is the equal to the current one, skip all such equal elements. Otherwise, add it to your new array", right?

    Hopefully the above "psudocode" points you in the right direction.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  5. #5
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by CodesAway View Post
    Oh, ok, got it. Well, in your code, you only skip the value if the next one is the same. This means, if there are multiple such values, the last one is stored (since the next value is different).

    Instead, what you want to do is "if the next element is the equal to the current one, skip all such equal elements. Otherwise, add it to your new array", right?

    Hopefully the above "psudocode" points you in the right direction.
    I understand your psudocode, however just to clarify,
    Java Code:
    if(temp == arr[val])
    continue;
    Is the problem child and I should not have the continue statement...

  6. #6
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by random0munky View Post
    I understand your psudocode, however just to clarify,
    Java Code:
    if(temp == arr[val])
    continue;
    Is the problem child and I should not have the continue statement...
    I'm not sure how to answer that question. You need a way to skip past ALL the occurrences of equal values, instead of just 1, like the above statement does. By skipping only one, the last occurrence will be stored. If you skip all the occurrences, none will be stored.

    Then, you need to handle the case when there is only one occurrence, since you only want to store the value if it occurs once.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  7. #7
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default

    I apologize for being so new to programming but I understand what you are telling me, but I just can't figure out how to put it in code form. I know when

    if(temp == arr[val])

    I did a println for temp and val and it prints out

    temp: 1, 1, 2, 2, 3
    val: 1, 2, 3, 4, 5

    I'm still not quite wrapping my head around how to just skip over what you said and just print the 3 and the 5.

  8. #8
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by random0munky View Post
    I apologize for being so new to programming but I understand what you are telling me, but I just can't figure out how to put it in code form. I know when

    if(temp == arr[val])

    I did a println for temp and val and it prints out

    temp: 1, 1, 2, 2, 3
    val: 1, 2, 3, 4, 5

    I'm still not quite wrapping my head around how to just skip over what you said and just print the 3 and the 5.
    Don't apologize for being new, we all started somewhere. I'm just glad you're asking for help instead of ramming your head against the wall.


    Ok, let's begin by breaking down the "psudocode" I gave, "if the next element is the equal to the current one, skip all such equal elements. Otherwise, add it to your new array".

    I took your original code, refactored it a little, and added in my above psudocode as comments. Your task is to write code to "skip all such equal elements".

    Java Code:
    import java.util.Arrays;
    
    class Test2
    {
    	public static void main(String args[])
    	{
    		int arr[] = new int[] { 2, 5, 1, 1, 2, 3 }; // 1, 1, 2, 2, 3, 5
    		Arrays.sort(arr);
    		// System.out.print(Arrays.toString(arr)) ;
    
    		int newArray[] = new int[arr.length];
    		int count = 0;
    
    		for (int i = 0; i < arr.length; i++) {
    			// if the next element is the equal to the current one
    			if (i + 1 < arr.length && arr[i + 1] == arr[i]) {
    				// skip all such equal elements
    			} else {
    				// Otherwise, add it to your new array
    				newArray[count++] = arr[i];
    			}
    			// System.out.print(temp + " ") ; // 1, 2, 3, 5
    		}
    
    		for (int i = 0; i < newArray.length; i++) {
    			System.out.print(newArray[i] + " ");
    		}
    	}
    }
    Last edited by CodesAway; 10-10-2009 at 09:43 PM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  9. #9
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default

    Yay!!! oh My Goodness. Your are absolutely getting some rep from me. Thank You oh so much. Also the cleaner looking code is much easier to read then what I first started. Now I just need to take out the 0's in the empty array elements.

  10. #10
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    I take it you got it working, I'm glad.

    If you have any further questions, don't hesitate to ask.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  11. #11
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default

    Yuppers I sure did. I deff. will post questions when the need arises.

Similar Threads

  1. Replies: 1
    Last Post: 08-07-2009, 12:14 PM
  2. Replies: 1
    Last Post: 11-03-2008, 02:38 PM
  3. Replies: 20
    Last Post: 05-14-2008, 09:42 AM
  4. number comparisson help
    By thegross1 in forum New To Java
    Replies: 3
    Last Post: 05-01-2008, 08:31 AM

Posting Permissions

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