McAfee Interview Question
Applications DevelopersCountry: United States
Interview Type: Written Test
Well that's pretty simple. You just need a char variable and a bool variable.
Store the first letter from the respective string in the char variable, then initialize the bool variable with, let's say, 1 (you have to do this every time you store a letter in the char variable).
Compare the letter with the rest of the letters (just the ones in front of said letter, if you want to be efficient); if you found an identical letter, change the bool variable with 0.
Repeat these two steps until either the bool variable remained unchanged (and the letter stored at that time is your answer), or you reached the end of the string (meaning there is no non-duplicate letter in that string)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s("efficiency");
bool* array = new bool[s.size()];
fill(array, array + s.size(), false);
bool* pb = array;
for (int i = 0; i < s.size() - 1; i++) {
for (int p = i + 1; p < s.size(); p++) {
if (s[i] == s[p]) {
array[i] = array[p] = true;
}
}
}
while(*pb) pb++;
cout << s[pb - array] << endl;
delete [] array;
return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s("efficiency");
bool* array = new bool[s.size()];
fill(array, array + s.size(), false);
bool* pb = array;
for (int i = 0; i < s.size() - 1; i++) {
for (int p = i + 1; p < s.size(); p++) {
if (s[i] == s[p]) {
array[i] = array[p] = true;
}
}
}
while(*pb) pb++;
cout << s[pb - array] << endl;
delete [] array;
return 0;
}
import java.util.Arrays;
/**
* @author Muhammad Tariq Akram
*/
public class FirstNonDuplicateLetter {
public static void main(String[] arg){
String str="12345123456789";
char[] myArray= str.toCharArray();
boolean isMatchFound=true;
for(int i=0; i<myArray.length; i++){
if (isMatchFound==false){
System.out.println("Result =" + myArray[i-1] );
break;
}
isMatchFound=false;
for(int j=i+1; j<myArray.length; j++){
if (myArray[i] == ' '){
isMatchFound = true;
break;
}else if (myArray[i] == myArray[j]){
myArray[j]= ' ';
isMatchFound = true;
break;
}
}
}
}
}
import java.util.Stack;
public class C {
public static void main(String[] args) {
String str = "efficiency";
char[] arr = str.toCharArray();
Stack<Character> st = new Stack<Character>();
Stack<Character> st1 = new Stack<Character>();
for(int i=0;i<arr.length-1;i++){
for(int j=i+1;j<arr.length-1;j++){
if(arr[i] == arr[j]){
st.push(arr[i]);
}
}
}
for(int i=0;i<arr.length;i++){
if(!(st.contains(arr[i]))){
st1.push(arr[i]);
}
}
System.out.println(st1);
}
}
import java.util.Stack;
public class C {
public static void main(String[] args) {
String str = "efficiency";
char[] arr = str.toCharArray();
Stack<Character> st = new Stack<Character>();
Stack<Character> st1 = new Stack<Character>();
for(int i=0;i<arr.length-1;i++){
for(int j=i+1;j<arr.length-1;j++){
if(arr[i] == arr[j]){
st.push(arr[i]);
}
}
}
for(int i=0;i<arr.length;i++){
if(!(st.contains(arr[i]))){
st1.push(arr[i]);
}
}
System.out.println(st1.elementAt(0));
}
}
public class FirstNonDuplicate
{
public static void main (String [] args)
{
char c = FindFirstNonDuplicate("efficiency");
if (c != '?')
System.out.println("First non-duplicate character is "+ c);
else
System.out.println("Error: There is no duplicate.");
}
public static char FindFirstNonDuplicate(String str)
{
str = str.trim();
str = str.toLowerCase();
int [] sum = new int[26];
for (int i=0; i<sum.length; i++)
sum[i] =0;
for (int i=0; i<str.length(); i++)
{
sum[str.charAt(i)-'a']++;
}
for (int i=0; i<sum.length; i++)
{
if (sum[i] == 1)
{
return (char)(i+'a');
}
}
return '?';
}
}
package nonDuplicate;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
public class FirstNonDuplicate {
/**
* @param args
*/
public static void main(String[] args) {
String s = "efficiency";
FirstNonDuplicate fND = new FirstNonDuplicate();
char firstNonDuplicate = fND.findFirstNonDuplicate(s);
System.out.println(firstNonDuplicate);
}
private Character findFirstNonDuplicate(String s){
if(s.length()==0){
return null;
}
Stack<Character> stack = new Stack<Character>();
List<Character> list = new LinkedList<Character>();
char[] characters = s.toCharArray();
for(int i=0;i<characters.length;i++){
stack.push(characters[i]);
}
while(!stack.isEmpty()){
Character tempChar = stack.pop();
if(list.contains(tempChar)){
list.remove(tempChar);
}else{
list.add(tempChar);
}
}
int size = list.size();
Character firstNonDuplicate = list.get(size-1);
return firstNonDuplicate;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input = "efficiency";
List<char> convertion = input.ToCharArray().ToList();
foreach (char ch in input.ToCharArray())
{
int count = convertion.Count(c => c == ch);
if (count == 1)
{
Console.WriteLine("output - " + ch);
break;
}
}
Console.Read();
}
}
}
1. Create a hash of chars found as key and their position in array as value: Hash<char,pos>
- Anonymous August 17, 20132. Parse each char at a time ,
- check if it exists in the hash , if yes mark the pos = -1 in hash table
-if it doesnt exist, add the char and its position to the hash.
3. parse the hash to check if a char has pos != -1 and is minimum - thats our answer