• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Stuck with Nth rarest element program

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){
Set<Integer> se = new HashSet<Integer>(lis);
Map<Integer,Integer> m = new HashMap<Integer,Integer>();
   for(int temp:se){
   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 :

(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 ?
Posts: 8880
Mac OS X 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).
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic