Expedia Group Interview Question: Describe and code an algorith... | Glassdoor

Interview Question

Software Development Engineer In Test (SDET) Interview Bellevue, WA

Describe and code an algorithm that returns the first

  duplicate character in a string?
Tags:
technical
Answer

Interview Answer

11 Answers

0

Simple Python example. Not sure it's most efficient.

def findDup(str):
    match=[]
    i=1
    while (i

Ben on Apr 30, 2012
5

first clarify if it is ASCII or UNICODE string
For ASCII, create BOOL checkArray [128] = {false};
walk the string and update the index of checkArray based of the character.

for (int index=0;index< strlen(str); index++)
{
if (checkArray[str[index]] == true)
{
  printf (str[index]);
  return;
}
else
{
    checkArray[str[index]] = true;
}
}

Eric on Jun 5, 2012
0

public class FirstDupCharacter {

    public static void main(String[] args) {
        System.out.println(findDupCharacter("abcdefghiaklmno"));
    }

    private static Character findDupCharacter(final String input) {
        final Set set = new HashSet();
        Character dup = null;
        for (int i = 0; i < input.length(); i++) {
            if (set.contains(input.charAt(i))) {
                dup = input.charAt(i);
                break;
            } else {
                set.add(input.charAt(i));
            }
        }
        return dup;
    }
}

Anonymous on Oct 19, 2012
0

String samp = "Testing";
        samp = samp.toLowerCase();
        char chararr[] = samp.toCharArray();
        int size = chararr.length;
        char repeat = ' ';
        for (int i=0;i

Pur on Jul 22, 2013
1

for (int i=0;i

Anonymous on Aug 13, 2013
1

public static in findDuplicateChar(String s) {

        if (s == null)
            return -1;

        char[] characters = s.toCharArray();

        Map charsMap = HashMap();

        for ( int index = 0; index < characters.length; index++ ) {
            // insert the character into the map.
            // returns null for a new entry
            // returns the index if it previously if it existed
            Integer initialOccurence = charsMap.put(characters[index], index);

            if ( initialOccurence != null) {
                return initialOccurance;
            }

            //there where no characters that where duplicates
            return -1;

        }
    }

Anonymous on Feb 4, 2015
0

Another python solution:

def findFirstNonRepeatedCharInOneIteration(str1):
    for i,j in enumerate(str1):
        if j in str1[:i] or j in str1[i+1:]:
            print "First non-repeated character is "+ j
            break

str1 = "abcdefglhjkkjokylf"
findFirstNonRepeatedCharInOneIteration(str1)

Anonymous on May 11, 2016
0

function getFirstDuplicateCharacter(str) {
  const seen = new Set();
  for (const char of str) {
    if (seen.has(char)) return char;
    seen.add(char);
  }
}

Anonymous on Sep 11, 2018
0

import java.io.*;
import java.util.*;

/*
 * code an algorithm that returns the first duplicate character in a string?
 */

class Solution {

  public static void main(String[] args) {
    String input = "";
    int j = removeduplicate(input);

    if(j == input.length()){

    System.out.println("String has unique characters" );

    }
    else{
    System.out.println("duplicate character is "+input.charAt(j));
    }

  }

    public static int removeduplicate(String input){

      Set set = new HashSet();
      boolean flag = false;
        int i =0;
        for(; i

Rupal Desai on Nov 13, 2018
0

import java.io.*;
import java.util.*;
class Solution {
    public static void main(String[] args) {
    String input = "";
    int j = removeduplicate(input);
        if(j == input.length()){
          System.out.println("String has unique characters" );
        }
    else{
    System.out.println("duplicate character is "+input.charAt(j));
    }
      }

  public static int removeduplicate(String input){
           Set set = new HashSet();
      boolean flag = false;
        int i =0;
        for(; i

Anonymous on Nov 13, 2018
0

public static int removeduplicate(String input){

      Set set = new HashSet();
      boolean flag = false;
        int i =0;
        for(; i

Rupal on Nov 13, 2018

Add Answers or Comments

To comment on this, Sign In or Sign Up.