ersachinjain83
BAN USER/*Solution: 1. Create a LinkedHashSet "set" that stores the Characters
* 2. Loop through the String.
* 3. Check if the ascii array has true for that character.
* 4. If yes then check if set contains the character. If yes then remove it.
*
* 3-b) If ascci array is false for that Character mark it true and add it to the set.
*
* 5. After the loop return the first element in the set if size of set > 0
*
* */
public static void problem3()
{
//String input = "tis ils khesktplhe";
String input = "abcedbacc";
Set<Character> set = new LinkedHashSet<>();
boolean[] ascii = new boolean[256];
for(char ch : input.toCharArray())
{
int index = (int) ch;
if(ascii[index])
{
if(set.contains(ch))
{
set.remove(ch);
}
}
else
{
ascii[index] = true;
set.add(ch);
}
}
for(Character ch : set)
{
System.out.println(ch);
break;
}
}
/*Solution: 1. Create a HashSet "set" of Integers.
- ersachinjain83 April 18, 2015* 2. Loop through the array of integers
* 3. If set contains the integer then remove it. This means even occurance of the number
* 4. If set does not contain the integer add the integer to the set. This means odd occurance of the number
* 5. Loop through the set and return the first element.
*
* */
public static void problem4()
{
int[] a = {4, 7, 2, 2, 5, 3, 5, 7, 7, 3, 4, 5, 7};
Set<Integer> set = new HashSet<>();
for(int number : a)
{
if(set.contains(number))
{
set.remove(number);
}
else
{
set.add(number);
}
}
for(int number : set)
{
System.out.println(number);
break;
}
}