counting occurrence of elements in a java array - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

counting occurrence of elements in a java array

bountibounti Posts: 14Member
What would be the best way to count how often a number (or several numbers) occurs in a java array ?

Thanks.

Comments

  • zibadianzibadian Posts: 6,349Member
    : What would be the best way to count how often a number (or several
    : numbers) occurs in a java array ?
    :
    : Thanks.
    If you want to count 1 number you can simply loop through the array and count how often the number is encountered.
    If the number of numbers you want to count is small (< ln(array.length)) then creating a loop around the previous loop is the easiest way. This is not the fastest way, but optimalized code doesn't give you a lot of additional speed in this case.
    If the number of numbers to count is very large (> ln(array.length)), then you should first sort the array (using Arrays.sort()). Then you can use the Arrays.binarySearch() to find the location of the number. From there you can "walk" up/down the array to find the start index and end index of the numbers. The number can be calculated using those indexes.
  • bar311bar311 Posts: 3Member
    : What would be the best way to count how often a number (or several
    : numbers) occurs in a java array ?
    :
    : Thanks.


    Try this one..... thanks!

    import java.util.*;

    // programmed in JDK 1.3
    public class FindOccurence {
    public static void main(String[] args) {
    System.out.println(
    find(new int[]{1,1,1,2,2,2,2,2,2,3,3,4,5,5,5,5}, 2)
    );
    }

    static int find(int[] arr, int numToFind) {
    int occurence=0;
    List l=new ArrayList();
    for (int i = 0; i < arr.length; i++) { l.add(new Integer(arr[i])); }
    if(l.contains(new Integer(numToFind))) {
    for(int i=0, size=l.size();i<size;i++)
    if(numToFind==((Integer)l.get(i)).intValue()) ++occurence;
    }
    return occurence;
    }
    }
  • zibadianzibadian Posts: 6,349Member
    : : What would be the best way to count how often a number (or several
    : : numbers) occurs in a java array ?
    : :
    : : Thanks.
    :
    :
    : Try this one..... thanks!
    :
    : import java.util.*;
    :
    : // programmed in JDK 1.3
    : public class FindOccurence {
    : public static void main(String[] args) {
    : System.out.println(
    : find(new int[]{1,1,1,2,2,2,2,2,2,3,3,4,5,5,5,5}, 2)
    : );
    : }
    :
    : static int find(int[] arr, int numToFind) {
    : int occurence=0;
    : List l=new ArrayList();
    : for (int i = 0; i < arr.length; i++) { l.add(new Integer(arr[i]));
    : }
    : if(l.contains(new Integer(numToFind))) {
    : for(int i=0, size=l.size();i<size;i++)
    : if(numToFind==((Integer)l.get(i)).intValue()) ++occurence;
    : }
    : return occurence;
    : }
    : }
    :
    Why not omit the creation of the ArrayList and use the array directly (it's faster)?
    [code]
    public static int find(int[] arr, int numToFind) {
    int occurence=0;
    for (int i = 0; i < arr.length; i++) {
    if (arr[i] == numToFind)
    occurence++;
    return occurence;
    }
    [/code]
  • bar311bar311 Posts: 3Member
    : : : What would be the best way to count how often a number (or several
    : : : numbers) occurs in a java array ?
    : : :
    : : : Thanks.
    : :
    : :
    : : Try this one..... thanks!
    : :
    : : import java.util.*;
    : :
    : : // programmed in JDK 1.3
    : : public class FindOccurence {
    : : public static void main(String[] args) {
    : : System.out.println(
    : : find(new int[]{1,1,1,2,2,2,2,2,2,3,3,4,5,5,5,5}, 2)
    : : );
    : : }
    : :
    : : static int find(int[] arr, int numToFind) {
    : : int occurence=0;
    : : List l=new ArrayList();
    : : for (int i = 0; i < arr.length; i++) { l.add(new Integer(arr[i]));
    : : }
    : : if(l.contains(new Integer(numToFind))) {
    : : for(int i=0, size=l.size();i<size;i++)
    : : if(numToFind==((Integer)l.get(i)).intValue()) ++occurence;
    : : }
    : : return occurence;
    : : }
    : : }
    : :
    : Why not omit the creation of the ArrayList and use the array
    : directly (it's faster)?
    : [code]:
    : public static int find(int[] arr, int numToFind) {
    : int occurence=0;
    : for (int i = 0; i < arr.length; i++) {
    : if (arr[i] == numToFind)
    : occurence++;
    : return occurence;
    : }
    : [/code]:


    yeah you're right about that... i must have overlooked that... coz i seldom use arrays when i'm programming... i got used to using the collections framework... =) tHANKS by the way.....

    By the way this algorithm if used on very large array capacity... will throw java.lang.OutOfMemoryError even the one in my post.....

    try this...


    public static void main(String[] args) {
    int[] large = new int[100000000];
    Arrays.fill(large, 1);
    System.out.println(find(large,1));
    }

    /*static int find(int[] arr, int numToFind) {
    int occurence=0;
    List l=new ArrayList();
    for (int i = 0; i < arr.length; i++) { l.add(new Integer(arr[i])); }
    if(l.contains(new Integer(numToFind))) {
    for(int i=0, size=l.size();i<size;i++)
    if(numToFind==((Integer)l.get(i)).intValue()) ++occurence;
    }
    return occurence;
    }*/

    public static int find(int[] arr, int numToFind) {
    int occurence=0;
    for (int i = 0; i < arr.length; i++)
    if (arr[i] == numToFind)
    occurence++;
    return occurence;
    }
  • zibadianzibadian Posts: 6,349Member
    : : : : What would be the best way to count how often a number (or several
    : : : : numbers) occurs in a java array ?
    : : : :
    : : : : Thanks.
    : : :
    : : :
    : : : Try this one..... thanks!
    : : :
    : : : import java.util.*;
    : : :
    : : : // programmed in JDK 1.3
    : : : public class FindOccurence {
    : : : public static void main(String[] args) {
    : : : System.out.println(
    : : : find(new int[]{1,1,1,2,2,2,2,2,2,3,3,4,5,5,5,5}, 2)
    : : : );
    : : : }
    : : :
    : : : static int find(int[] arr, int numToFind) {
    : : : int occurence=0;
    : : : List l=new ArrayList();
    : : : for (int i = 0; i < arr.length; i++) { l.add(new Integer(arr[i]));
    : : : }
    : : : if(l.contains(new Integer(numToFind))) {
    : : : for(int i=0, size=l.size();i<size;i++)
    : : : if(numToFind==((Integer)l.get(i)).intValue()) ++occurence;
    : : : }
    : : : return occurence;
    : : : }
    : : : }
    : : :
    : : Why not omit the creation of the ArrayList and use the array
    : : directly (it's faster)?
    : : [code]: :
    : : public static int find(int[] arr, int numToFind) {
    : : int occurence=0;
    : : for (int i = 0; i < arr.length; i++) {
    : : if (arr[i] == numToFind)
    : : occurence++;
    : : return occurence;
    : : }
    : : [/code]: :
    :
    :
    : yeah you're right about that... i must have overlooked that... coz i
    : seldom use arrays when i'm programming... i got used to using the
    : collections framework... =) tHANKS by the way.....
    :
    : By the way this algorithm if used on very large array capacity...
    : will throw java.lang.OutOfMemoryError even the one in my post.....
    :
    : try this...
    :
    :
    : public static void main(String[] args) {
    : int[] large = new int[100000000];
    : Arrays.fill(large, 1);
    : System.out.println(find(large,1));
    : }
    :
    : /*static int find(int[] arr, int numToFind) {
    : int occurence=0;
    : List l=new ArrayList();
    : for (int i = 0; i < arr.length; i++) { l.add(new Integer(arr[i]));
    : }
    : if(l.contains(new Integer(numToFind))) {
    : for(int i=0, size=l.size();i<size;i++)
    : if(numToFind==((Integer)l.get(i)).intValue()) ++occurence;
    : }
    : return occurence;
    : }*/
    :
    : public static int find(int[] arr, int numToFind) {
    : int occurence=0;
    : for (int i = 0; i < arr.length; i++)
    : if (arr[i] == numToFind)
    : occurence++;
    : return occurence;
    : }
    :
    That's only logical. Each int is 4 byte large. All the elements together are almost 400 MB in size, which is a lot larger than the standard JVM memory space. If you need more space you can request it using one of the java.exe command line options.
    Side-note: your code will require a lot more. When the internal array of the ArrayList is too small, a bigger one is created and then the original is copied. Also since each object is essentially a 32-bit pointer to some memory location, each element in the ArrayList uses at least 8 bytes. This together with the copy action will mean that you will need at least 16 bytes per element. With an array of 100,000,000 elements, your code needs at least about 2.4 GB of memory!
Sign In or Register to comment.