java – How To Find 2nd or any other order,say 3rd, 4th, non repeating character in a string?

java – How To Find 2nd or any other order,say 3rd, 4th, non repeating character in a string?

Simply iterate over the string and put the occurrence of each character in a map with the key is the character, and the value is an object which has two attributes the number of occurence and the first occurence position.

Then you simple sort the keySet() of this map depending on the position and iterate over the sorted set till u find an object with only one occurence

   for(int i=0;i<str.length();i++) {
       if(map.get(str.chatAt(i))==null) {
           map.put(str.charAt(i),new Wrapper(i,1)) ;
        } else {
           map.get(str.charAt(i)).occurence++;
        }

   }



     class Wrapper {
        public int occurrence;
        public int position;
        public Wrapper(int position,int occurrence) {

        this.position = position;
        this.occurrence = occurrence;
       }


      }

Use LinkedHashMap

Hash table and linked list implementation of the Map interface, with predictable iteration order. … This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map.

With this, it becomes easy:

// We keep only a map of between tha char and the number of
// occurrences. We dont store the positions for the next (after first)
// occurrences, but the LinkedHashMap guarantees that
// the iteration order is the same as  insertion order
// (so first inserted letters will appear first when iterating)
LinkedHashMap<Character, Integer> charOccurs=new LinkedHashMap<>();

for(char c : str) {
  Integer occ=charOccurs.get(c);
  if(null==occ) {
    occ=0;
  }
  else {
    occ=new Integer(occ.intValue()+1);
  }
  // Note that insertion order is not affected 
  // if a key is re-inserted into the map.
  // Niiice!
  charOccurs.put(c, occ);
}

int order=1;
for(Map.Entry<Character, Integer> entry : charOccurs.entrySet()) {
  // this works because ...
  // This linked list defines the iteration ordering,
  // which is normally the order in which keys were inserted
  // Doubly-nice!!!
  if(1==entry.getValue()) { // single occurrence
    System.out.println(Char: +entry.getKey()+ order: +order);
    order++; 
  }
}

java – How To Find 2nd or any other order,say 3rd, 4th, non repeating character in a string?

I think you will like to go through old skool method

String intitialString = statistic non;
        int count = 0;
        char[] ch = intitialString.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            for (int j = 0; j < ch.length; j++) {
                if (ch[i] == ch[j]) {
                    count++;
                }
            }
            if (count > 1) {
                count = 0;
            } else
                System.out.println(ch[i]);
        }

output

a
c
o

Hope this helps!

Leave a Reply

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