42
int[] a = new int[10]{1,2,3,4,5,6,7,7,7,7};

how can I write a method and return 7?

I want to keep it native without the help of lists, maps or other helpers. Only arrays[].

1

27 Answers 27

89

Try this answer. First, the data:

int[] a = {1,2,3,4,5,6,7,7,7,7};

Here, we build a map counting the number of times each number appears:

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i : a) {
    Integer count = map.get(i);
    map.put(i, count != null ? count+1 : 1);
}

Now, we find the number with the maximum frequency and return it:

Integer popular = Collections.max(map.entrySet(),
    new Comparator<Map.Entry<Integer, Integer>>() {
    @Override
    public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
        return o1.getValue().compareTo(o2.getValue());
    }
}).getKey();

As you can see, the most popular number is seven:

System.out.println(popular);
> 7

EDIT

Here's my answer without using maps, lists, etc. and using only arrays; although I'm sorting the array in-place. It's O(n log n) complexity, better than the O(n^2) accepted solution.

public int findPopular(int[] a) {

    if (a == null || a.length == 0)
        return 0;

    Arrays.sort(a);

    int previous = a[0];
    int popular = a[0];
    int count = 1;
    int maxCount = 1;

    for (int i = 1; i < a.length; i++) {
        if (a[i] == previous)
            count++;
        else {
            if (count > maxCount) {
                popular = a[i-1];
                maxCount = count;
            }
            previous = a[i];
            count = 1;
        }
    }

    return count > maxCount ? a[a.length-1] : popular;

}
3
  • 3
    It is not a good idea to return 0 in case of null reference or length of 0 due to the high possibility of 0 being a valid element in such situation.
    – Haggra
    Aug 21, 2016 at 14:29
  • In the map that counts the number of occurrences, if the count is null, means that the element found is the first occurrence, so why not put 1 instead of 0? Jul 3, 2020 at 20:47
  • @MassimilianoGiunchi you're right! thanks for discovering that bug, it's fixed now Jul 3, 2020 at 21:05
40
public int getPopularElement(int[] a)
{
  int count = 1, tempCount;
  int popular = a[0];
  int temp = 0;
  for (int i = 0; i < (a.length - 1); i++)
  {
    temp = a[i];
    tempCount = 0;
    for (int j = 1; j < a.length; j++)
    {
      if (temp == a[j])
        tempCount++;
    }
    if (tempCount > count)
    {
      popular = temp;
      count = tempCount;
    }
  }
  return popular;
}
6
  • 7
    thanks for the solution. I was surprised only because your answer was accepted as the correct answer and generally in computer science o(n2) isnt the best answer. but your solution is definitely easier to understand simple and intuitive. man you have got a great score on Stackoverflow. kudos!!! Jun 26, 2014 at 18:44
  • @gabhi: I just made that straight away after looking at the OP's question. Never thought much on the complexity front, though if you can make one, I be happy to upvote that straight away :-)
    – nIcE cOw
    Jun 26, 2014 at 18:57
  • 3
    I guess putting frequency of each integer in a hash table can bring it down to linear time.
    – human.js
    Dec 30, 2015 at 22:01
  • @nIcEcOw even someone has to go with two loops, i see scope of improvisation here. See mine answer
    – M Sach
    Aug 27, 2016 at 13:53
  • will your code have a lot of changes if we work with an arbitrary string given as input instead of an integer string? (an arbitrary string like: 'today is 6th august aaa', so the most popular letter is a)
    – user8422515
    Aug 7, 2017 at 3:21
8
  1. Take a map to map element - > count
  2. Iterate through array and process the map
  3. Iterate through map and find out the popular
6
  • Good answer. You may want to mention that if the numbers in the original are limited to a relatively small max (say, 100 or 1000) you could use an array instead of a map. Dec 17, 2011 at 15:09
  • @dasb Map is fine I don't see any disadvantage of it against any advantages of array Dec 17, 2011 at 15:12
  • how can i do it native? with the help of temp array only
    – SexyMF
    Dec 17, 2011 at 15:16
  • @dasb Coders new to Java often find arrays easier to understand than maps. Considering the wording (and indeed the content of the question) there is no doubt in my mind that the OP is new to programming (and not just to programming in Java). This is very likely his homework. Dec 17, 2011 at 15:17
  • You have a data array now what you need is count array, for example if your 0th element in data array is 1 and first is 2 so in oyur count array you should hold 1 that is the count for 1 and so on.. Dec 17, 2011 at 15:20
7

Assuming your array is sorted (like the one you posted) you could simply iterate over the array and count the longest segment of elements, it's something like @narek.gevorgyan's post but without the awfully big array, and it uses the same amount of memory regardless of the array's size:

private static int getMostPopularElement(int[] a){
    int counter = 0, curr, maxvalue, maxcounter = -1;
    maxvalue = curr = a[0];

    for (int e : a){
        if (curr == e){
            counter++;
        } else {
            if (counter > maxcounter){
                maxcounter = counter;
                maxvalue = curr;
            }
            counter = 0;
            curr = e;
        }
    }
    if (counter > maxcounter){
        maxvalue = curr;
    }

    return maxvalue;
}


public static void main(String[] args) {
    System.out.println(getMostPopularElement(new int[]{1,2,3,4,5,6,7,7,7,7}));
}

If the array is not sorted, sort it with Arrays.sort(a);

6

Using Java 8 Streams

int data[] = { 1, 5, 7, 4, 6, 2, 0, 1, 3, 2, 2 };
Map<Integer, Long> count = Arrays.stream(data)
    .boxed()
    .collect(Collectors.groupingBy(Function.identity(), counting()));

int max = count.entrySet().stream()
    .max((first, second) -> {
        return (int) (first.getValue() - second.getValue());
    })
    .get().getKey();

System.out.println(max);

Explanation

We convert the int[] data array to boxed Integer Stream. Then we collect by groupingBy on the element and use a secondary counting collector for counting after the groupBy.

Finally we sort the map of element -> count based on count again by using a stream and lambda comparator.

3

This one without maps:

public class Main {       

    public static void main(String[] args) {
        int[] a = new int[]{ 1, 2, 3, 4, 5, 6, 7, 7, 7, 7 };
        System.out.println(getMostPopularElement(a));        
    }

    private static int getMostPopularElement(int[] a) {             
        int maxElementIndex = getArrayMaximumElementIndex(a); 
        int[] b = new int[a[maxElementIndex] + 1]

        for (int i = 0; i < a.length; i++) {
            ++b[a[i]];
        }

        return getArrayMaximumElementIndex(b);
    }

    private static int getArrayMaximumElementIndex(int[] a) {
        int maxElementIndex = 0;

        for (int i = 1; i < a.length; i++) {
            if (a[i] >= a[maxElementIndex]) {
                maxElementIndex = i;
            }
        }

        return maxElementIndex;
    }      

}

You only have to change some code if your array can have elements which are < 0. And this algorithm is useful when your array items are not big numbers.

2

If you don't want to use a map, then just follow these steps:

  1. Sort the array (using Arrays.sort())
  2. Use a variable to hold the most popular element (mostPopular), a variable to hold its number of occurrences in the array (mostPopularCount), and a variable to hold the number of occurrences of the current number in the iteration (currentCount)
  3. Iterate through the array. If the current element is the same as mostPopular, increment currentCount. If not, reset currentCount to 1. If currentCount is > mostPopularCount, set mostPopularCount to currentCount, and mostPopular to the current element.
2
  • Yeah, it is obviously better than my answer. Dec 17, 2011 at 15:35
  • But maybe his array has very big size with small numbers. In that case mine is better. Dec 17, 2011 at 15:36
2

Seems like you are looking for the Mode value (Statistical Mode) , have a look at Apache's Docs for Statistical functions.

2
package frequent;

import java.util.HashMap;
import java.util.Map;

public class Frequent_number {

    //Find the most frequent integer in an array

    public static void main(String[] args) {
        int arr[]= {1,2,3,4,3,2,2,3,3};

        System.out.println(getFrequent(arr));
        System.out.println(getFrequentBySorting(arr));
    }

    //Using Map , TC: O(n)  SC: O(n)
    static public int getFrequent(int arr[]){
        int ans=0;
        Map<Integer,Integer> m = new HashMap<>();
        for(int i:arr){
            if(m.containsKey(i)){
                m.put(i, m.get(i)+1);
            }else{
                m.put(i, 1);
            }
        }
        int maxVal=0;
        for(Integer in: m.keySet()){
            if(m.get(in)>maxVal){
                ans=in;
                maxVal = m.get(in);
            }
        }
        return ans;
    }

    //Sort the array and then find it TC: O(nlogn) SC: O(1)
    public static int getFrequentBySorting(int arr[]){
        int current=arr[0];
        int ansCount=0;
        int tempCount=0;
        int ans=current;
        for(int i:arr){
            if(i==current){
                tempCount++;
            }
            if(tempCount>ansCount){
                ansCount=tempCount;
                ans=i;
            }
            current=i;
        }
        return ans;
    }

}
2

Array elements value should be less than the array length for this one:

public void findCounts(int[] arr, int n) {
    int i = 0;

    while (i < n) {
        if (arr[i] <= 0) {
            i++;
            continue;
        }

        int elementIndex = arr[i] - 1;

        if (arr[elementIndex] > 0) {
            arr[i] = arr[elementIndex];
            arr[elementIndex] = -1;
        }
        else {
            arr[elementIndex]--;
            arr[i] = 0;
            i++;
        }
    }

    Console.WriteLine("Below are counts of all elements");

    for (int j = 0; j < n; j++) {
        Console.WriteLine(j + 1 + "->" + Math.Abs(arr[j]));
    }
}

Time complexity of this will be O(N) and space complexity will be O(1).

1
import java.util.Scanner;


public class Mostrepeatednumber
{
    public static void main(String args[])
    {
        int most = 0;
        int temp=0;
        int count=0,tempcount;
        Scanner in=new Scanner(System.in);
        System.out.println("Enter any number");
        int n=in.nextInt();
        int arr[]=new int[n];
        System.out.print("Enter array value:");
        for(int i=0;i<=n-1;i++)
        {
            int n1=in.nextInt();
            arr[i]=n1;
        }
        //!!!!!!!! user input concept closed
        //logic can be started
        for(int j=0;j<=n-1;j++)
        {
        temp=arr[j];
        tempcount=0;
            for(int k=1;k<=n-1;k++)
                {
                if(temp==arr[k])
                    {
                        tempcount++;
                    }   
                        if(count<tempcount)
                            {
                                most=arr[k];
                                    count=tempcount;
                            }
                }

        }
        System.out.println(most);
    }

}
1
  • 1
    please add some explanation to your code - this will help others who see your answer in the future Feb 19, 2015 at 10:53
1

Best approach will be using map where key will be element and value will be the count of each element. Along with that keep an array of size that will contain the index of most popular element . Populate this array while map construction itself so that we don't have to iterate through map again.

Approach 2:-

If someone want to go with two loop, here is the improvisation from accepted answer where we don't have to start second loop from one every time

public class TestPopularElements {
    public static int getPopularElement(int[] a) {
        int count = 1, tempCount;
        int popular = a[0];
        int temp = 0;
        for (int i = 0; i < (a.length - 1); i++) {
            temp = a[i];
            tempCount = 0;
            for (int j = i+1; j < a.length; j++) {
                if (temp == a[j])
                    tempCount++;
            }
            if (tempCount > count) {
                popular = temp;
                count = tempCount;
            }
        }
        return popular;
    }

    public static void main(String[] args) {
        int a[] = new int[] {1,2,3,4,5,6,2,7,7,7};

        System.out.println("count is " +getPopularElement(a));
    }

}
1

Assuming your int array is sorted, i would do...

int count = 0, occur = 0, high = 0, a;

for (a = 1; a < n.length; a++) {
    if (n[a - 1] == n[a]) {
       count++;
       if (count > occur) {
           occur = count;
           high = n[a];
       }
     } else {
        count = 0;
     }
}
System.out.println("highest occurence = " + high);
1
public static int getMostCommonElement(int[] array) {

    Arrays.sort(array);

    int frequency = 1;
    int biggestFrequency = 1;
    int mostCommonElement = 0;

    for(int i=0; i<array.length-1; i++) {
        frequency = (array[i]==array[i+1]) ? frequency+1 : 1;
        if(frequency>biggestFrequency) {
            biggestFrequency = frequency; 
            mostCommonElement = array[i];
        }
    }

    return mostCommonElement;
}
1
  • Note that sorting the array produces side effects.
    – jcrs
    Jul 12, 2018 at 11:12
1

Mine Linear O(N)

Using map to save all the differents elements found in the array and saving the number of times ocurred, then just getting the max from the map.

import java.util.HashMap;
import java.util.Map;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.IntStream;

public class MosftOftenNumber {

    // for O(N) + map O(1) = O(N) 
    public static int mostOftenNumber(int[] a)
    {
        final Map m = new HashMap<Integer,Integer>();
        int max = 0;
        int element = 0;

        for (int i=0; i<a.length; i++){
            //initializing value for the map the value will have the counter of each element
            //first time one new number its found will be initialize with zero 
            if (m.get(a[i]) == null)
                m.put(a[i],0);

            //save each value from the array and increment the count each time its found
            m.put(a[i] , (Integer) m.get(a[i]) + 1);

            //check the value from each element and comparing with max
            if ( (Integer) m.get(a[i]) > max){
                max = (Integer) m.get(a[i]);
                element = a[i];
            }

        }
        System.out.println("Times repeated: " + max);
        return element;
    }

    public static int mostOftenNumberWithLambdas(int[] a)
    {
        Integer max = IntStream.of(a).boxed().max(Integer::compareTo).get();
        Integer coumtMax = Math.toIntExact(IntStream.of(a).boxed().filter(number -> number.equals(max)).count());
        System.out.println("Times repeated: " + coumtMax);
        return max;
    }

    public static void main(String args[]) {
//      int[] array = {1,1,2,1,1};
//      int[] array = {2,2,1,2,2};
        int[] array = {1,2,3,4,5,6,7,7,7,7};
        System.out.println("Most often number with loops: " + mostOftenNumber(array));
        System.out.println("Most often number with lambdas: " + mostOftenNumberWithLambdas(array));
    }

}
3
  • Please explain your answer rather than just showing the code Apr 16, 2016 at 22:48
  • this does not even compile Apr 16, 2016 at 22:55
  • now compiles i let some code in the main uncomment and also add some more comments inside the function. Apr 17, 2016 at 0:02
1

Solution 1: Hashmap: O(n)

def most_freq_elem(arr):
    frequency = {}
    most_frequent, most_count = -1, 0

    for num in arr:
        frequency[num] = frequency.get(num, 0) + 1

        if frequency[num] > most_count:
            most_count = frequency[num]
            most_frequent = num

    return most_frequent

Solution 2: Hashmap + Max: O(n)

def most_freq_elem(arr):
    frequency = {}

    for num in arr:
        frequency[num] = frequency.get(num, 0) + 1

    return max(frequency, key=frequency.get)
0
public static void main(String[] args) {

    int[] myArray = {1,5,4,4,22,4,9,4,4,8};
    Map<Integer,Integer> arrayCounts = new HashMap<>();
    Integer popularCount  = 0;
    Integer popularValue = 0;

    for(int i = 0; i < myArray.length; i++) {
        Integer count = arrayCounts.get(myArray[i]);
        if (count == null) {
            count = 0;
        }
        arrayCounts.put(myArray[i], count == 0 ? 1 : ++count);
        if (count > popularCount) {
            popularCount = count;
            popularValue = myArray[i];
        }
    }

    System.out.println(popularValue + " --> " + popularCount);
}
0

below code can be put inside a main method

    // TODO Auto-generated method stub
    Integer[] a = { 11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 1, 2, 2, 2, 2, 3, 4, 2 };
    List<Integer> list = new ArrayList<Integer>(Arrays.asList(a));
    Set<Integer> set = new HashSet<Integer>(list);
    int highestSeq = 0;
    int seq = 0;
    for (int i : set) {
        int tempCount = 0;
        for (int l : list) {
            if (i == l) {
                tempCount = tempCount + 1;
            }
            if (tempCount > highestSeq) {
                highestSeq = tempCount;
                seq = i;
            }
        }

    }

    System.out.println("highest sequence is " + seq + " repeated for " + highestSeq);
0
public class MostFrequentIntegerInAnArray {

    public static void main(String[] args) {
        int[] items = new int[]{2,1,43,1,6,73,5,4,65,1,3,6,1,1};
        System.out.println("Most common item = "+getMostFrequentInt(items));
    }

    //Time Complexity = O(N)
    //Space Complexity = O(N)
    public static int getMostFrequentInt(int[] items){
        Map<Integer, Integer> itemsMap = new HashMap<Integer, Integer>(items.length);
        for(int item : items){
            if(!itemsMap.containsKey(item))
                itemsMap.put(item, 1);
            else
                itemsMap.put(item, itemsMap.get(item)+1);
        }

        int maxCount = Integer.MIN_VALUE;
        for(Entry<Integer, Integer> entry : itemsMap.entrySet()){
            if(entry.getValue() > maxCount)
                maxCount = entry.getValue();
        }
        return maxCount;
    }
}
0
int largest = 0;
int k = 0;
for (int i = 0; i < n; i++) {
    int count = 1;
    for (int j = i + 1; j < n; j++) {
        if (a[i] == a[j]) {
            count++;
        }
    }
    if (count > largest) {
        k = a[i];
        largest = count;
    }
}

So here n is the length of the array, and a[] is your array.

First, take the first element and check how many times it is repeated and increase the counter (count) as to see how many times it occurs. Check if this maximum number of times that a number has so far occurred if yes, then change the largest variable (to store the maximum number of repetitions) and if you would like to store the variable as well, you can do so in another variable (here k).

I know this isn't the fastest, but definitely, the easiest way to understand

5
  • 1
    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
    – Clijsters
    Mar 6, 2018 at 12:33
  • @Clijsters done so now,but still new to this platform and I unable to write multi-line comments here
    – MathMan
    Mar 6, 2018 at 12:42
  • Comments can't be multiline. If you mean the answer text, it is markdown; that means you need two returns for a new paragraph. This is explained at the small help box.
    – Clijsters
    Mar 6, 2018 at 12:43
  • Ohh, but then I use this /* */ for multiline comments?
    – MathMan
    Mar 6, 2018 at 12:44
  • You might want to take a look at Hos do I write a good answer?. I dont' get what you mean. Multiline comments in java? Yes, that's correct.
    – Clijsters
    Mar 6, 2018 at 12:44
0
import java.util.HashMap;
import java.util.Map;
import java.lang.Integer;
import java.util.Iterator;
public class FindMood {
    public static void main(String [] args){
    int arrayToCheckFrom [] = {1,2,4,4,5,5,5,3,3,3,3,3,3,3,3};
    Map map = new HashMap<Integer, Integer>();
    for(int i = 0 ; i < arrayToCheckFrom.length; i++){
    int sum = 0;
      for(int k = 0 ; k < arrayToCheckFrom.length ; k++){
          if(arrayToCheckFrom[i]==arrayToCheckFrom[k])
          sum += 1; 
      }
      map.put(arrayToCheckFrom[i], sum);
    }
    System.out.println(getMaxValue(map));
}
  public static Integer getMaxValue( Map<Integer,Integer> map){
        Map.Entry<Integer,Integer> maxEntry = null;
        Iterator iterator = map.entrySet().iterator();  
        while(iterator.hasNext()){
            Map.Entry<Integer,Integer> pair = (Map.Entry<Integer,Integer>) iterator.next();
            if(maxEntry == null || pair.getValue().compareTo(maxEntry.getValue())>0){
                maxEntry = pair; 
            } 
        }
        return maxEntry.getKey();
    }
}
0

Comparing two arrays, I Hope for that this is useful for you.

public static void main(String []args){

        int primerArray [] = {1,2,1,3,5};
        int arrayTow [] = {1,6,7,8};


       int numberMostRepetly =  validateArrays(primerArray,arrayTow);

       System.out.println(numberMostRepetly);


}


public static int validateArrays(int primerArray[], int arrayTow[]){

    int numVeces = 0;

    for(int i = 0; i< primerArray.length; i++){

        for(int c = i+1; c < primerArray.length; c++){

            if(primerArray[i] == primerArray[c]){
                numVeces = primerArray[c];
                // System.out.println("Numero que mas se repite");
                //System.out.println(numVeces);
            }
        }

        for(int a = 0; a < arrayTow.length; a++){

            if(numVeces == arrayTow[a]){
               // System.out.println(numVeces);
                return numVeces;
            }
        }
    }

    return 0;

}
0
0
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    import java.util.Map.Entry;
    
    public class MostOccuringEleementInArrayOfIntegers {
    
        public static void main(String[] args) {
    
            int[] arr = { 1, 2, 3, 4, 4, 5, 3, 2, 1, 6, 7, 1, 2, 3, 2 };
    
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    
            for (int num : arr) {
    
                if (map.containsKey(num)) {
                    map.put(num, map.get(num) + 1);
                } else {
                    map.put(num, 1);
                }
    
            }
    
            Set<Entry<Integer, Integer>> entrySet = map.entrySet();
    
            int max = 1;
            int mostFrequent = 1;
    
        for (Entry<Integer, Integer> e : map.entrySet()) {
//      Swapping of the elements who is occuring most
            if (e.getValue() > max) {
                mostFrequent = e.getKey();
                max = e.getValue();
            }
        }

//      Print most frequent element
        System.out.println("Most frequent element: " + mostFrequent);

       }
    }
-1
public class MostFrequentNumber {

    public MostFrequentNumber() {

    }

    int frequentNumber(List<Integer> list){

        int popular = 0;
        int holder = 0;

        for(Integer number: list) {
            int freq = Collections.frequency(list,number);

            if(holder < freq){
                holder = freq;
                popular = number;
            }
        }

       return popular;

    }

    public static void main(String[] args){

        int[] numbers = {4,6,2,5,4,7,6,4,7,7,7};

        List<Integer> list = new ArrayList<Integer>();

        for(Integer num : numbers){
            list.add(num);
        }


        MostFrequentNumber mostFrequentNumber = new MostFrequentNumber();

        System.out.println(mostFrequentNumber.frequentNumber(list));


    }
}
0
-1

You can count the occurrences of the different numbers, then look for the highest one. This is an example that uses a Map, but could relatively easily be adapted to native arrays.

Second largest element: Let us take example : [1,5,4,2,3] in this case, Second largest element will be 4.

  1. Sort the Array in descending order, once the sort done output will be A = [5,4,3,2,1]

  2. Get the Second Largest Element from the sorted array Using Index 1. A[1] -> Which will give the Second largest element 4.

private static int getMostOccuringElement(int[] A) { Map occuringMap = new HashMap();

    //count occurences
    for (int i = 0; i < A.length; i++) { 
        if (occuringMap.get(A[i]) != null) {
            int val = occuringMap.get(A[i]) + 1;
            occuringMap.put(A[i], val);
        } else {
            occuringMap.put(A[i], 1);
        }
    }

    //find maximum occurence
    int max = Integer.MIN_VALUE; 
    int element = -1;
    for (Map.Entry<Integer, Integer> entry : occuringMap.entrySet()) {
        if (entry.getValue() > max) {
            max = entry.getValue();
            element = entry.getKey();
        }
    }
    return element;
}
4
  • Please add some explanation to your code for the benefit of a complete answer! Feb 14, 2018 at 15:19
  • Second largest element: Let us take example : [1,5,4,2,3] in this case, Second largest element will be 4. 1. Sort the Array in decending order, once the sort done output will be A = [5,4,3,2,1] 2. Get the Second Largest Element from the sorted array Using Index 1. A[1] -> Which will give the Second larget element 4 Feb 15, 2018 at 6:10
  • @ArnavBorborah hope the explanation fine Feb 15, 2018 at 10:27
  • I meant that you should probably take what you wrote in that comment and add it to your answer. (Using the edit button). Feb 15, 2018 at 11:36
-1

I hope this helps.

public class Ideone {
    public static void main(String[] args) throws java.lang.Exception {


        int[] a = {1,2,3,4,5,6,7,7,7};
        int len = a.length;

        System.out.println(len);


        for (int i = 0; i <= len - 1; i++) {

            while (a[i] == a[i + 1]) {
                System.out.println(a[i]);

                break;
            }


        }


    }


}
-3

This is the wrong syntax. When you create an anonymous array you MUST NOT give its size.

When you write the following code :

    new int[] {1,23,4,4,5,5,5};

You are here creating an anonymous int array whose size will be determined by the number of values that you provide in the curly braces.

You can assign this a reference as you have done, but this will be the correct syntax for the same :-

    int[] a = new int[]{1,2,3,4,5,6,7,7,7,7};

Now, just Sysout with proper index position:

    System.out.println(a[7]);
2
  • 1
    I seriously doubt that that's the OP's question. Dec 17, 2011 at 15:12
  • He wants the most frequent number. Not this. Mar 30, 2019 at 19:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.