Results 1 to 8 of 8
  1. #1
    djbeko06 is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default How to find the number of unique values in an Array

    How can I find the number of unique values of a String array?
    e.g.
    double a[] = {aab,aba,aca,aab,adc,zzz,aca};
    How can I get 2?

    Many thank

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: How to find the number of unique values in an Array

    What classes and methods can you use for this?
    Sort the array and then scan it
    Use a Map to keep a count of the occurences of each value then scan it

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

    Default Re: How to find the number of unique values in an Array

    Quote Originally Posted by djbeko06 View Post
    e.g.
    double a[] = {aab,aba,aca,aab,adc,zzz,aca};
    Do you mean String a[] = {"aab","aba","aca","aab","adc","zzz","aca"}; ? :)

    How can I get 2?
    System.out.println(a.length-new HashSet<String>(Arrays.asList(a)).size());
    :P

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: How to find the number of unique values in an Array

    eRaaaa You could explain how your very clever code works in case the OP is a beginner and not familiar with the techniques you are using.

    Does your code produce the correct answer? See next post.
    Last edited by Norm; 09-11-2011 at 05:17 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: How to find the number of unique values in an Array

    Go thru the list adding one to count if unique, subtracting 1 if a duplicate:
    "aab", 1
    "aba", 2
    "aca", 3
    "aab", 2 (-1 because a dup)
    "adc", 3
    "zzz", 4
    "aca" 3 (-1 dup)

    Are there 2 or 3 unique elements in the list? aba, adc and zzz

  6. #6
    djbeko06 is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: How to find the number of unique values in an Array

    Sorry this is the full question:

    Create a static method called countUnique(String s[]) that accepts an array of strings as a parameter and returns the number of unique strings in the array. A unique string would be a string that occurs only once in the array. For example countUnique(new int[]{“aaa”,”aba”,”aba”,”ccc”}) would return 2 as there are 2 unique strings in the array: “aaa” and “ccc”

  7. #7
    srivempala is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default Re: How to find the number of unique values in an Array

    I think better to put it in set. then set will not allow duplicates so u can get unique values and count those elament su can get 2.

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

    Default Re: How to find the number of unique values in an Array

    Quote Originally Posted by djbeko06 View Post
    Sorry this is the full question:

    Create a static method called countUnique(String s[]) that accepts an array of strings as a parameter and returns the number of unique strings in the array. A unique string would be a string that occurs only once in the array. For example countUnique(new int[]{“aaa”,”aba”,”aba”,”ccc”}) would return 2 as there are 2 unique strings in the array: “aaa” and “ccc”
    Use two Set<String> collections; one to store each element in your array and the other one to store the duplicates (already present in the first set). At the end remove everything in the first set that is also present in the second set.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. find the unique values of a double array
    By tyang in forum New To Java
    Replies: 3
    Last Post: 09-11-2011, 02:47 PM
  2. Find the First Negative Number of an Array
    By Cod in forum New To Java
    Replies: 12
    Last Post: 02-20-2011, 05:59 PM
  3. Find the second largest number in the array
    By radhi16 in forum New To Java
    Replies: 5
    Last Post: 01-13-2011, 05:08 PM
  4. Find max number in 2D array?
    By spatel14 in forum New To Java
    Replies: 3
    Last Post: 06-30-2010, 04:27 PM
  5. Replies: 8
    Last Post: 04-22-2008, 03:21 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
  •