• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Stuck with Nth rarest element program

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class NthRareElemnt {

public static void main(String args[]){

int arr[]={2,2,3,1,1,4,4,4,3,5};
ArrayList<Integer> lis = new ArrayList<Integer>();
for(int i :arr){
lis.add(i);
}
Set<Integer> se = new HashSet<Integer>(lis);
Map<Integer,Integer> m = new HashMap<Integer,Integer>();
   for(int temp:se){
   
    m.put(temp,Collections.frequency(lis,temp));
   }
   Set<Entry<Integer,Integer>> set = m.entrySet();
   ArrayList<Entry<Integer,Integer>> al = new ArrayList<Entry<Integer,Integer>>(set);
   Comparator<Entry<Integer,Integer>> c = new Comparator<Entry<Integer,Integer>>(){
public int compare(Entry<Integer, Integer> o1,Entry<Integer, Integer> o2) {
// TODO Auto-generated method stub
Integer i1 = o1.getValue();
Integer i2 = o2.getValue();
return i1.compareTo(i2);
}
   };
   Collections.sort(al, c);
   for(Entry<Integer,Integer> e: al){
    System.out.println(e.getKey()+ "----"+e.getValue());
   }
   
}

}


output :
5----1
2----2
1----2
3----2
4----3

(element -> frequency in sorted order)

I try to do the program like this .But i'm stuck after this . Could someone please help me to proceed further ?
 
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You said you stuck, but you didn't explain what stopped you.

One more thing. When you post code, don't forget to use code tags. Simply select the code snippet and hit the Code button in the text assembling panel.

An example how code would look like using code tags:

And compare with the version without the code tags, basically as you have:

public class Test {

   public static void main(String[] args) {
       System.out.println("Hello, World!");
   }
}

As you see former version is much better to read, understand and discuss (as it has line numbers and adds some colour coding).
 
This is my favorite tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic