find the frequency of elements in a java array

后端 未结 9 1499
灰色年华
灰色年华 2020-12-03 16:40

I have an int array:

{1,2,4,2,3,5,6,4,3}

How can I find frequencies of array elements like 1=1,2=2,3=2,4=4... I need a class w

相关标签:
9条回答
  • 2020-12-03 16:51

    You have to do a few things:

    1. Define an upper and lower bound for your range of numbers.
    2. Establish a convenient object/data structure to store occurrences of these numbers.
    3. Iterate through the passed-in array and count all occurrences of each number, storing the result in the convenient object/data structure.

    If this is done in a simple manner, it could be only a matter of reading the elements from the passed-in array and printing out the final result.

    0 讨论(0)
  • 2020-12-03 16:52
    import java.util.*;
    class Findfreqarray
    {
        public static void main(String args[])
        {
            int t, i, j, len, count=0;
            Scanner in = new Scanner(System.in);
            System.out.println("Enter number of elements to insert in an array: ");
            len = in.nextInt();
            int[] arr = new int[len];
            System.out.println("Enter elements to insert in an array: ");
            for(i=0;i<len;i++)
            {
                t = in.nextInt();
                arr[i] = t;
            }
            System.out.println("\n");
            for(i=0;i<len;i++)
            {
                count=1;
                for(j=i+1;j<=len-1;j++)
                {
                    if(arr[i]==arr[j] && arr[i]!='\0')
                    {
                        count++;
                        arr[j] = '\0';
                    }
                }
                if(arr[i]!='\0')
                {
                    System.out.println(arr[i] + " is " + count + " times.\n");
                }
            }        
        }
    }
    
    0 讨论(0)
  • 2020-12-03 17:05

    Without giving it away here is a good starting point:

    int[] array = {1,2,4,2,3,5,6,4,3};
    
            public int[] (array){
                //need to perform a sort...or a search
                //after searching check for matches,
                //sorting could make performing comparisons more efficient
                //not all searches/sorts are created equal.
    
                int[array.length] result += {"["+numberChecked+"]="+freqOccurred};
                return result;
            }
    

    This code is has not been compiled so think of it more as psuedocode. The purpose is to get you thinking about how to achieve the desired goal. An java package may already exist that can check the frequency elements in an array, but this is what you are most likely looking for. Good Luck.

    0 讨论(0)
  • 2020-12-03 17:07

    In Java 8 you can do this

    Map<Integer, Long> freq = Arrays.stream(array).boxed().
                    collect(Collectors.groupingBy(Integer::intValue, Collectors.counting()));
    
    0 讨论(0)
  • 2020-12-03 17:10

    You can also try below code,

        Map<Integer, Integer> map = new HashMap<>();
        int arr[] = new int[]{2, 2, 3, 3, 4, 5, 6, 7, 9, 9, 0, 4, 2};
    
        for (int i = 0; i < arr.length; i++) {
            Integer count = map.getOrDefault(arr[i], 0);
            map.put(arr[i], count + 1);
        }
    
        //Print the map to see the occurrence
        map.forEach((key, value) -> {
            System.out.println(key + " -> " + value);
        });
    
    0 讨论(0)
  • 2020-12-03 17:13

    Using Java-8 we can find the frequency of an array in a single line.

    Map<Integer, Long> freq = Arrays.stream(a).boxed().
                              collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    

    Also, since the question demands we need to return an array

    public Object[] getFrequencies(int[] a) {
        Map<Integer, Long> freq = Arrays.stream(a).boxed().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        return freq.entrySet().toArray();
    }
    
    0 讨论(0)
提交回复
热议问题