Find missing element in an array












3















I've an array consisting english letter a to z except one letter. I dont know which element is missing and I need to find that element using only one loop. I cant use if statements and inbuilt functions.



Is it really possible?. I got this question in an exam and couldn't find any solution and its eating my brain. Any insights appreciated.



// missing 'h'
var letters = ['a','b','c','d','e','f','g','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];

for (var i = 0; i < letters.length; i++) {
// find missing letter in 1 loop with no if statements or built-in functions
}









share|improve this question




















  • 4





    Is it javascript or c#?

    – Jabberwocky
    Nov 21 '18 at 17:27






  • 1





    I can use either javascript or c#

    – Optimus
    Nov 21 '18 at 17:27











  • Does the array have an empty character? Can you use it compared to another array (e.g. the actual alphabet)? You need to have something to cross check.

    – Jabberwocky
    Nov 21 '18 at 17:28








  • 8





    If you give each number a value from 1 to 25 and then total all the number. The total for all 25 would be 325. Then then missing one is 325 - total.

    – jdweng
    Nov 21 '18 at 17:32








  • 1





    What does "can't use any conditions" even mean? Your for loop will have a condition underneath. Does that mean you can't write any if statements yourself?

    – Sam Arustamyan
    Nov 21 '18 at 17:35
















3















I've an array consisting english letter a to z except one letter. I dont know which element is missing and I need to find that element using only one loop. I cant use if statements and inbuilt functions.



Is it really possible?. I got this question in an exam and couldn't find any solution and its eating my brain. Any insights appreciated.



// missing 'h'
var letters = ['a','b','c','d','e','f','g','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];

for (var i = 0; i < letters.length; i++) {
// find missing letter in 1 loop with no if statements or built-in functions
}









share|improve this question




















  • 4





    Is it javascript or c#?

    – Jabberwocky
    Nov 21 '18 at 17:27






  • 1





    I can use either javascript or c#

    – Optimus
    Nov 21 '18 at 17:27











  • Does the array have an empty character? Can you use it compared to another array (e.g. the actual alphabet)? You need to have something to cross check.

    – Jabberwocky
    Nov 21 '18 at 17:28








  • 8





    If you give each number a value from 1 to 25 and then total all the number. The total for all 25 would be 325. Then then missing one is 325 - total.

    – jdweng
    Nov 21 '18 at 17:32








  • 1





    What does "can't use any conditions" even mean? Your for loop will have a condition underneath. Does that mean you can't write any if statements yourself?

    – Sam Arustamyan
    Nov 21 '18 at 17:35














3












3








3


1






I've an array consisting english letter a to z except one letter. I dont know which element is missing and I need to find that element using only one loop. I cant use if statements and inbuilt functions.



Is it really possible?. I got this question in an exam and couldn't find any solution and its eating my brain. Any insights appreciated.



// missing 'h'
var letters = ['a','b','c','d','e','f','g','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];

for (var i = 0; i < letters.length; i++) {
// find missing letter in 1 loop with no if statements or built-in functions
}









share|improve this question
















I've an array consisting english letter a to z except one letter. I dont know which element is missing and I need to find that element using only one loop. I cant use if statements and inbuilt functions.



Is it really possible?. I got this question in an exam and couldn't find any solution and its eating my brain. Any insights appreciated.



// missing 'h'
var letters = ['a','b','c','d','e','f','g','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];

for (var i = 0; i < letters.length; i++) {
// find missing letter in 1 loop with no if statements or built-in functions
}






c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 18:27









mhodges

7,68311231




7,68311231










asked Nov 21 '18 at 17:24









OptimusOptimus

1,24262858




1,24262858








  • 4





    Is it javascript or c#?

    – Jabberwocky
    Nov 21 '18 at 17:27






  • 1





    I can use either javascript or c#

    – Optimus
    Nov 21 '18 at 17:27











  • Does the array have an empty character? Can you use it compared to another array (e.g. the actual alphabet)? You need to have something to cross check.

    – Jabberwocky
    Nov 21 '18 at 17:28








  • 8





    If you give each number a value from 1 to 25 and then total all the number. The total for all 25 would be 325. Then then missing one is 325 - total.

    – jdweng
    Nov 21 '18 at 17:32








  • 1





    What does "can't use any conditions" even mean? Your for loop will have a condition underneath. Does that mean you can't write any if statements yourself?

    – Sam Arustamyan
    Nov 21 '18 at 17:35














  • 4





    Is it javascript or c#?

    – Jabberwocky
    Nov 21 '18 at 17:27






  • 1





    I can use either javascript or c#

    – Optimus
    Nov 21 '18 at 17:27











  • Does the array have an empty character? Can you use it compared to another array (e.g. the actual alphabet)? You need to have something to cross check.

    – Jabberwocky
    Nov 21 '18 at 17:28








  • 8





    If you give each number a value from 1 to 25 and then total all the number. The total for all 25 would be 325. Then then missing one is 325 - total.

    – jdweng
    Nov 21 '18 at 17:32








  • 1





    What does "can't use any conditions" even mean? Your for loop will have a condition underneath. Does that mean you can't write any if statements yourself?

    – Sam Arustamyan
    Nov 21 '18 at 17:35








4




4





Is it javascript or c#?

– Jabberwocky
Nov 21 '18 at 17:27





Is it javascript or c#?

– Jabberwocky
Nov 21 '18 at 17:27




1




1





I can use either javascript or c#

– Optimus
Nov 21 '18 at 17:27





I can use either javascript or c#

– Optimus
Nov 21 '18 at 17:27













Does the array have an empty character? Can you use it compared to another array (e.g. the actual alphabet)? You need to have something to cross check.

– Jabberwocky
Nov 21 '18 at 17:28







Does the array have an empty character? Can you use it compared to another array (e.g. the actual alphabet)? You need to have something to cross check.

– Jabberwocky
Nov 21 '18 at 17:28






8




8





If you give each number a value from 1 to 25 and then total all the number. The total for all 25 would be 325. Then then missing one is 325 - total.

– jdweng
Nov 21 '18 at 17:32







If you give each number a value from 1 to 25 and then total all the number. The total for all 25 would be 325. Then then missing one is 325 - total.

– jdweng
Nov 21 '18 at 17:32






1




1





What does "can't use any conditions" even mean? Your for loop will have a condition underneath. Does that mean you can't write any if statements yourself?

– Sam Arustamyan
Nov 21 '18 at 17:35





What does "can't use any conditions" even mean? Your for loop will have a condition underneath. Does that mean you can't write any if statements yourself?

– Sam Arustamyan
Nov 21 '18 at 17:35












3 Answers
3






active

oldest

votes


















3














Here is code :



             char inputs = { 'a', 'b', 'c', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

int total = 0;
foreach (char c in inputs)
{
total += (int)c - (int)'a';
}
char missingChar = (char)((int)('a') + (325 - total));

// version 2
total = 0;
//from sum of a arithmetic sequence for even number of values
// for example 1 + 2 + 3 + 4 = (1 + 4) + (2 + 3) = 2 * 5 = 10
int expectedTotal = 13 * ((int)'a' + (int)'z');
foreach (char c in inputs)
{
total += (int)c;
}
missingChar = (char)((expectedTotal - total));





share|improve this answer


























  • Seriously man.. now I can sleep

    – Optimus
    Nov 21 '18 at 18:05











  • Super creative solution

    – roozbeh S
    Nov 21 '18 at 20:12











  • I didn't event this. Learned it ages ago. The trick of programming is learning algorithms, remembering the algorithms, then applying algorithms to current programming task.

    – jdweng
    Nov 21 '18 at 22:15











  • I added a version 2 of the code which is even simpler than original.

    – jdweng
    Nov 22 '18 at 21:17



















0














Here is another way of doing it without using any loop just list functions(Console Application) it also take care of lower upper case comparison issue:



namespace MissingAlphbetLetters
{
class Program
{

public static List<char> CheckDifferences(char str)
{
List<char> alphabet= new List<char>(new char
{ 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'});
List<char> compaired = str.ToList().ConvertAll(l => char.ToLower(l));
List<char> difference = new List<char>();
if (str.Length > 0)
{
difference = alphabet.Except(compaired).ToList();
}
return difference;
}


static void Main(string args)
{
Console.Write(string.Join(",",CheckDifferences(new char { 'A', 'g', 't', 'h', 'o', 'm', 'd', 'e' })));
Console.ReadLine();
}
}
}





share|improve this answer































    0














     class Program
    {
    private static void FindLetter(string alphabet, string yourMissingLetter)
    {
    for (var i = 0; i < yourMissingLetter.Length; i++)
    {
    var isit = alphabet[i] == yourMissingLetter[i];
    Console.WriteLine("alphabet[{0}] = {1}", alphabet[i], isit);
    }
    }
    static void Main(string args)
    {
    string alphabet = { "A", "B", "C", "D" };
    // let's say your array is missing B
    string yourMissingLetterArray = { "A", "C", "D" };
    FindLetter(alphabet, yourMissingLetterArray);
    }
    }


    not as brilliant as the actual answer but this will work:



    The moment it starts missing the letter will turn false and the index will show you which letter it is.






    share|improve this answer


























    • This only works if the array is sorted. The actual answer work if the array is not sorted.

      – jdweng
      Nov 26 '18 at 10:09











    • @jdweng they asked the guy if the array is sorted and he said yes. So this works.

      – Jabberwocky
      Nov 26 '18 at 19:35











    • Did you test your code? How many print statements does your code output?

      – jdweng
      Nov 26 '18 at 20:15











    • @jdweng Assuming it would be the whole alphabet it would print 26 and once you've reached the false, you'd look at the index and find which letter it is. or vice versa. To answer your question, now it prints 3 times.

      – Jabberwocky
      Nov 27 '18 at 17:05













    • And what letters does it say that it is missing?

      – jdweng
      Nov 27 '18 at 17:11











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417551%2ffind-missing-element-in-an-array%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Here is code :



                 char inputs = { 'a', 'b', 'c', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

    int total = 0;
    foreach (char c in inputs)
    {
    total += (int)c - (int)'a';
    }
    char missingChar = (char)((int)('a') + (325 - total));

    // version 2
    total = 0;
    //from sum of a arithmetic sequence for even number of values
    // for example 1 + 2 + 3 + 4 = (1 + 4) + (2 + 3) = 2 * 5 = 10
    int expectedTotal = 13 * ((int)'a' + (int)'z');
    foreach (char c in inputs)
    {
    total += (int)c;
    }
    missingChar = (char)((expectedTotal - total));





    share|improve this answer


























    • Seriously man.. now I can sleep

      – Optimus
      Nov 21 '18 at 18:05











    • Super creative solution

      – roozbeh S
      Nov 21 '18 at 20:12











    • I didn't event this. Learned it ages ago. The trick of programming is learning algorithms, remembering the algorithms, then applying algorithms to current programming task.

      – jdweng
      Nov 21 '18 at 22:15











    • I added a version 2 of the code which is even simpler than original.

      – jdweng
      Nov 22 '18 at 21:17
















    3














    Here is code :



                 char inputs = { 'a', 'b', 'c', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

    int total = 0;
    foreach (char c in inputs)
    {
    total += (int)c - (int)'a';
    }
    char missingChar = (char)((int)('a') + (325 - total));

    // version 2
    total = 0;
    //from sum of a arithmetic sequence for even number of values
    // for example 1 + 2 + 3 + 4 = (1 + 4) + (2 + 3) = 2 * 5 = 10
    int expectedTotal = 13 * ((int)'a' + (int)'z');
    foreach (char c in inputs)
    {
    total += (int)c;
    }
    missingChar = (char)((expectedTotal - total));





    share|improve this answer


























    • Seriously man.. now I can sleep

      – Optimus
      Nov 21 '18 at 18:05











    • Super creative solution

      – roozbeh S
      Nov 21 '18 at 20:12











    • I didn't event this. Learned it ages ago. The trick of programming is learning algorithms, remembering the algorithms, then applying algorithms to current programming task.

      – jdweng
      Nov 21 '18 at 22:15











    • I added a version 2 of the code which is even simpler than original.

      – jdweng
      Nov 22 '18 at 21:17














    3












    3








    3







    Here is code :



                 char inputs = { 'a', 'b', 'c', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

    int total = 0;
    foreach (char c in inputs)
    {
    total += (int)c - (int)'a';
    }
    char missingChar = (char)((int)('a') + (325 - total));

    // version 2
    total = 0;
    //from sum of a arithmetic sequence for even number of values
    // for example 1 + 2 + 3 + 4 = (1 + 4) + (2 + 3) = 2 * 5 = 10
    int expectedTotal = 13 * ((int)'a' + (int)'z');
    foreach (char c in inputs)
    {
    total += (int)c;
    }
    missingChar = (char)((expectedTotal - total));





    share|improve this answer















    Here is code :



                 char inputs = { 'a', 'b', 'c', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

    int total = 0;
    foreach (char c in inputs)
    {
    total += (int)c - (int)'a';
    }
    char missingChar = (char)((int)('a') + (325 - total));

    // version 2
    total = 0;
    //from sum of a arithmetic sequence for even number of values
    // for example 1 + 2 + 3 + 4 = (1 + 4) + (2 + 3) = 2 * 5 = 10
    int expectedTotal = 13 * ((int)'a' + (int)'z');
    foreach (char c in inputs)
    {
    total += (int)c;
    }
    missingChar = (char)((expectedTotal - total));






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 21:22

























    answered Nov 21 '18 at 18:03









    jdwengjdweng

    17.4k2717




    17.4k2717













    • Seriously man.. now I can sleep

      – Optimus
      Nov 21 '18 at 18:05











    • Super creative solution

      – roozbeh S
      Nov 21 '18 at 20:12











    • I didn't event this. Learned it ages ago. The trick of programming is learning algorithms, remembering the algorithms, then applying algorithms to current programming task.

      – jdweng
      Nov 21 '18 at 22:15











    • I added a version 2 of the code which is even simpler than original.

      – jdweng
      Nov 22 '18 at 21:17



















    • Seriously man.. now I can sleep

      – Optimus
      Nov 21 '18 at 18:05











    • Super creative solution

      – roozbeh S
      Nov 21 '18 at 20:12











    • I didn't event this. Learned it ages ago. The trick of programming is learning algorithms, remembering the algorithms, then applying algorithms to current programming task.

      – jdweng
      Nov 21 '18 at 22:15











    • I added a version 2 of the code which is even simpler than original.

      – jdweng
      Nov 22 '18 at 21:17

















    Seriously man.. now I can sleep

    – Optimus
    Nov 21 '18 at 18:05





    Seriously man.. now I can sleep

    – Optimus
    Nov 21 '18 at 18:05













    Super creative solution

    – roozbeh S
    Nov 21 '18 at 20:12





    Super creative solution

    – roozbeh S
    Nov 21 '18 at 20:12













    I didn't event this. Learned it ages ago. The trick of programming is learning algorithms, remembering the algorithms, then applying algorithms to current programming task.

    – jdweng
    Nov 21 '18 at 22:15





    I didn't event this. Learned it ages ago. The trick of programming is learning algorithms, remembering the algorithms, then applying algorithms to current programming task.

    – jdweng
    Nov 21 '18 at 22:15













    I added a version 2 of the code which is even simpler than original.

    – jdweng
    Nov 22 '18 at 21:17





    I added a version 2 of the code which is even simpler than original.

    – jdweng
    Nov 22 '18 at 21:17













    0














    Here is another way of doing it without using any loop just list functions(Console Application) it also take care of lower upper case comparison issue:



    namespace MissingAlphbetLetters
    {
    class Program
    {

    public static List<char> CheckDifferences(char str)
    {
    List<char> alphabet= new List<char>(new char
    { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'});
    List<char> compaired = str.ToList().ConvertAll(l => char.ToLower(l));
    List<char> difference = new List<char>();
    if (str.Length > 0)
    {
    difference = alphabet.Except(compaired).ToList();
    }
    return difference;
    }


    static void Main(string args)
    {
    Console.Write(string.Join(",",CheckDifferences(new char { 'A', 'g', 't', 'h', 'o', 'm', 'd', 'e' })));
    Console.ReadLine();
    }
    }
    }





    share|improve this answer




























      0














      Here is another way of doing it without using any loop just list functions(Console Application) it also take care of lower upper case comparison issue:



      namespace MissingAlphbetLetters
      {
      class Program
      {

      public static List<char> CheckDifferences(char str)
      {
      List<char> alphabet= new List<char>(new char
      { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'});
      List<char> compaired = str.ToList().ConvertAll(l => char.ToLower(l));
      List<char> difference = new List<char>();
      if (str.Length > 0)
      {
      difference = alphabet.Except(compaired).ToList();
      }
      return difference;
      }


      static void Main(string args)
      {
      Console.Write(string.Join(",",CheckDifferences(new char { 'A', 'g', 't', 'h', 'o', 'm', 'd', 'e' })));
      Console.ReadLine();
      }
      }
      }





      share|improve this answer


























        0












        0








        0







        Here is another way of doing it without using any loop just list functions(Console Application) it also take care of lower upper case comparison issue:



        namespace MissingAlphbetLetters
        {
        class Program
        {

        public static List<char> CheckDifferences(char str)
        {
        List<char> alphabet= new List<char>(new char
        { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'});
        List<char> compaired = str.ToList().ConvertAll(l => char.ToLower(l));
        List<char> difference = new List<char>();
        if (str.Length > 0)
        {
        difference = alphabet.Except(compaired).ToList();
        }
        return difference;
        }


        static void Main(string args)
        {
        Console.Write(string.Join(",",CheckDifferences(new char { 'A', 'g', 't', 'h', 'o', 'm', 'd', 'e' })));
        Console.ReadLine();
        }
        }
        }





        share|improve this answer













        Here is another way of doing it without using any loop just list functions(Console Application) it also take care of lower upper case comparison issue:



        namespace MissingAlphbetLetters
        {
        class Program
        {

        public static List<char> CheckDifferences(char str)
        {
        List<char> alphabet= new List<char>(new char
        { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'});
        List<char> compaired = str.ToList().ConvertAll(l => char.ToLower(l));
        List<char> difference = new List<char>();
        if (str.Length > 0)
        {
        difference = alphabet.Except(compaired).ToList();
        }
        return difference;
        }


        static void Main(string args)
        {
        Console.Write(string.Join(",",CheckDifferences(new char { 'A', 'g', 't', 'h', 'o', 'm', 'd', 'e' })));
        Console.ReadLine();
        }
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 18:37









        GeorgeTGeorgeT

        114




        114























            0














             class Program
            {
            private static void FindLetter(string alphabet, string yourMissingLetter)
            {
            for (var i = 0; i < yourMissingLetter.Length; i++)
            {
            var isit = alphabet[i] == yourMissingLetter[i];
            Console.WriteLine("alphabet[{0}] = {1}", alphabet[i], isit);
            }
            }
            static void Main(string args)
            {
            string alphabet = { "A", "B", "C", "D" };
            // let's say your array is missing B
            string yourMissingLetterArray = { "A", "C", "D" };
            FindLetter(alphabet, yourMissingLetterArray);
            }
            }


            not as brilliant as the actual answer but this will work:



            The moment it starts missing the letter will turn false and the index will show you which letter it is.






            share|improve this answer


























            • This only works if the array is sorted. The actual answer work if the array is not sorted.

              – jdweng
              Nov 26 '18 at 10:09











            • @jdweng they asked the guy if the array is sorted and he said yes. So this works.

              – Jabberwocky
              Nov 26 '18 at 19:35











            • Did you test your code? How many print statements does your code output?

              – jdweng
              Nov 26 '18 at 20:15











            • @jdweng Assuming it would be the whole alphabet it would print 26 and once you've reached the false, you'd look at the index and find which letter it is. or vice versa. To answer your question, now it prints 3 times.

              – Jabberwocky
              Nov 27 '18 at 17:05













            • And what letters does it say that it is missing?

              – jdweng
              Nov 27 '18 at 17:11
















            0














             class Program
            {
            private static void FindLetter(string alphabet, string yourMissingLetter)
            {
            for (var i = 0; i < yourMissingLetter.Length; i++)
            {
            var isit = alphabet[i] == yourMissingLetter[i];
            Console.WriteLine("alphabet[{0}] = {1}", alphabet[i], isit);
            }
            }
            static void Main(string args)
            {
            string alphabet = { "A", "B", "C", "D" };
            // let's say your array is missing B
            string yourMissingLetterArray = { "A", "C", "D" };
            FindLetter(alphabet, yourMissingLetterArray);
            }
            }


            not as brilliant as the actual answer but this will work:



            The moment it starts missing the letter will turn false and the index will show you which letter it is.






            share|improve this answer


























            • This only works if the array is sorted. The actual answer work if the array is not sorted.

              – jdweng
              Nov 26 '18 at 10:09











            • @jdweng they asked the guy if the array is sorted and he said yes. So this works.

              – Jabberwocky
              Nov 26 '18 at 19:35











            • Did you test your code? How many print statements does your code output?

              – jdweng
              Nov 26 '18 at 20:15











            • @jdweng Assuming it would be the whole alphabet it would print 26 and once you've reached the false, you'd look at the index and find which letter it is. or vice versa. To answer your question, now it prints 3 times.

              – Jabberwocky
              Nov 27 '18 at 17:05













            • And what letters does it say that it is missing?

              – jdweng
              Nov 27 '18 at 17:11














            0












            0








            0







             class Program
            {
            private static void FindLetter(string alphabet, string yourMissingLetter)
            {
            for (var i = 0; i < yourMissingLetter.Length; i++)
            {
            var isit = alphabet[i] == yourMissingLetter[i];
            Console.WriteLine("alphabet[{0}] = {1}", alphabet[i], isit);
            }
            }
            static void Main(string args)
            {
            string alphabet = { "A", "B", "C", "D" };
            // let's say your array is missing B
            string yourMissingLetterArray = { "A", "C", "D" };
            FindLetter(alphabet, yourMissingLetterArray);
            }
            }


            not as brilliant as the actual answer but this will work:



            The moment it starts missing the letter will turn false and the index will show you which letter it is.






            share|improve this answer















             class Program
            {
            private static void FindLetter(string alphabet, string yourMissingLetter)
            {
            for (var i = 0; i < yourMissingLetter.Length; i++)
            {
            var isit = alphabet[i] == yourMissingLetter[i];
            Console.WriteLine("alphabet[{0}] = {1}", alphabet[i], isit);
            }
            }
            static void Main(string args)
            {
            string alphabet = { "A", "B", "C", "D" };
            // let's say your array is missing B
            string yourMissingLetterArray = { "A", "C", "D" };
            FindLetter(alphabet, yourMissingLetterArray);
            }
            }


            not as brilliant as the actual answer but this will work:



            The moment it starts missing the letter will turn false and the index will show you which letter it is.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 27 '18 at 17:20

























            answered Nov 21 '18 at 18:11









            JabberwockyJabberwocky

            1238




            1238













            • This only works if the array is sorted. The actual answer work if the array is not sorted.

              – jdweng
              Nov 26 '18 at 10:09











            • @jdweng they asked the guy if the array is sorted and he said yes. So this works.

              – Jabberwocky
              Nov 26 '18 at 19:35











            • Did you test your code? How many print statements does your code output?

              – jdweng
              Nov 26 '18 at 20:15











            • @jdweng Assuming it would be the whole alphabet it would print 26 and once you've reached the false, you'd look at the index and find which letter it is. or vice versa. To answer your question, now it prints 3 times.

              – Jabberwocky
              Nov 27 '18 at 17:05













            • And what letters does it say that it is missing?

              – jdweng
              Nov 27 '18 at 17:11



















            • This only works if the array is sorted. The actual answer work if the array is not sorted.

              – jdweng
              Nov 26 '18 at 10:09











            • @jdweng they asked the guy if the array is sorted and he said yes. So this works.

              – Jabberwocky
              Nov 26 '18 at 19:35











            • Did you test your code? How many print statements does your code output?

              – jdweng
              Nov 26 '18 at 20:15











            • @jdweng Assuming it would be the whole alphabet it would print 26 and once you've reached the false, you'd look at the index and find which letter it is. or vice versa. To answer your question, now it prints 3 times.

              – Jabberwocky
              Nov 27 '18 at 17:05













            • And what letters does it say that it is missing?

              – jdweng
              Nov 27 '18 at 17:11

















            This only works if the array is sorted. The actual answer work if the array is not sorted.

            – jdweng
            Nov 26 '18 at 10:09





            This only works if the array is sorted. The actual answer work if the array is not sorted.

            – jdweng
            Nov 26 '18 at 10:09













            @jdweng they asked the guy if the array is sorted and he said yes. So this works.

            – Jabberwocky
            Nov 26 '18 at 19:35





            @jdweng they asked the guy if the array is sorted and he said yes. So this works.

            – Jabberwocky
            Nov 26 '18 at 19:35













            Did you test your code? How many print statements does your code output?

            – jdweng
            Nov 26 '18 at 20:15





            Did you test your code? How many print statements does your code output?

            – jdweng
            Nov 26 '18 at 20:15













            @jdweng Assuming it would be the whole alphabet it would print 26 and once you've reached the false, you'd look at the index and find which letter it is. or vice versa. To answer your question, now it prints 3 times.

            – Jabberwocky
            Nov 27 '18 at 17:05







            @jdweng Assuming it would be the whole alphabet it would print 26 and once you've reached the false, you'd look at the index and find which letter it is. or vice versa. To answer your question, now it prints 3 times.

            – Jabberwocky
            Nov 27 '18 at 17:05















            And what letters does it say that it is missing?

            – jdweng
            Nov 27 '18 at 17:11





            And what letters does it say that it is missing?

            – jdweng
            Nov 27 '18 at 17:11


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417551%2ffind-missing-element-in-an-array%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            Npm cannot find a required file even through it is in the searched directory

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith