Coding Fun Java Java Interview Question Part 2 Implementation | Sorting of Array Depending on Frequency of Elements

Java Interview Question Part 2 Implementation | Sorting of Array Depending on Frequency of Elements





You are given any array of length n.
You have to sort the array elements in descending order of their frequency.
Use of Collections is not allowed

E.g :
Input : [4,3,1,6,4,3,6,4]
Output : [4,4,4,3,3,6,6,1]

Check out our website:

Follow Telusko on Twitter:

Follow on Facebook:
Telusko :
Navin Reddy :

Follow Navin Reddy on Instagram:…

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

13 thoughts on “Java Interview Question Part 2 Implementation | Sorting of Array Depending on Frequency of Elements”

  1. Moin says:

    Very very poor explanation. Please remove this and put a new video. You're clearly under prepared here.

  2. Please try to complete explanation of a problem in one video

  3. Duy Bui says:

    There is a flaw here I believe, you only increment by 1 if the element is repeated, you do b[x][1] = b[x][1] + 1; but you missed the initial count for element that only appears once

  4. Sweet Swathi says:

    Why haven't u used Arrays.sort?

  5. Hi Naveen,

    The kind of explanation you are using to explain the technique is totally not understandable.
    May be you should practice teaching before uploading/recording the video

    @Naveen

  6. Niket Kumar says:

    Please, there are better approaches than this, you could have used BST or hash

  7. praveen Bi says:

    Hallo Navin,
    int[] array = {2,4,2,2,3,6,9,9,9,9};

    int element = 0;
    int count = 0;

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

    int tempElement = array[i];
    int tempCount= 0;
    for(int j = 0; j < array.length; j++) {
    if(array[j] == tempElement)
    tempCount++;
    }
    i did up to hear, After that how should i sort them in dependency Bcoz all the frequency values is there in "tempCount++" where should i store them and how should i call them to sort?

    I think for this no need of two dimentional "System.out.println(" " + element + "—> " + count);"

  8. praveen Bi says:

    Hallo Navin, Sorry to say, Seriously i didn't understand what you are saying, And i need clear explanation about frequency of elements sorting in dependency i need to know if possible can you please help me

  9. Nikola Tesla says:

    need more videos like these…thanks Telusko!

  10. Narendra R says:

    Hi Navin,
    When element is added for first time you are saying default .but where are you mentioning it?

  11. Anil kumar says:

    /* complete code for the program */
    public class SortFreq {

    static int x=0 ;

    public static void main(String[] args) {

    int a[] = {4,3,1,6,4,3,6,4};
    int b[][]= new int[a.length][2];

    a = sort(a);

    /*for(int i :a){
    System.out.print(i+",");
    }
    */

    b[x][0]=a[0];
    for(int i=1;i<a.length;i++){
    if(a[i]==a[i-1]){
    b[x][1]=b[x][1]+1;
    }
    else{
    x++;
    b[x][0]=a[i];
    }
    }
    x++;

    b =sort(b);

    for(int i=0;i<x;i++){

    for(int j=0;j<=b[i][1];j++){
    System.out.print(" "+b[i][0]);
    }
    }

    System.out.println("");
    }

    public static int[] sort(int a[]){

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

    for(int j=i;j<a.length;j++){

    if(a[i]>a[j]){

    int t ;
    t =a[i];
    a[i]=a[j];
    a[j]=t;
    }
    }
    }

    return a;
    }

    public static int[][] sort(int b[][]){

    int c[]= new int[2];

    for(int i=0;i<x;i++){

    for(int j=i;j<x;j++){
    if(b[i][1]<b[j][1]){

    c=b[i];
    b[i]=b[j];
    b[j]=c;
    }
    }
    }

    return b;
    }

    }

Leave a Reply

Your email address will not be published. Required fields are marked *