package simpleproblems;

import java.util.LinkedHashMap;

public class FirstDuplicate {
	public static char[] firstDuplicate(String s) {
		char[] r = new char[2];  // result = [first non duplicate, first duplicate]
		LinkedHashMap<Character, Integer> m = new LinkedHashMap<Character, Integer>();  // Linked Hash Map for maintained insertion order.
		for (char c: s.toCharArray()) {
			Integer count = m.get(c);
			if (count == null) {
				m.put(c, 1);
			} else {
				m.put(c, count + 1);
			}
		}
		for (char c: m.keySet()) {
			if (m.get(c) == 1) {
				r[0] = c;
				break;
			}
		}
		for (char c: m.keySet()) {
			if (m.get(c) > 1) {
				r[1] = c;
				break;
			}
		}
		return r;
	}
	// Using a normal hashmap, i can do for(c in s): then extract from the hashmap.

	public static void main(String[] args) {
		System.out.println(FirstDuplicate.firstDuplicate("abcdbc"));
	}
}
