Home > Enterprise >  c# finding even or odd numbers
c# finding even or odd numbers

Time:03-15

Question: You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N. Write a method that takes the array as an argument and returns this "outlier" N.

Examples [2, 4, 0, 100, 4, 11, 2602, 36] Should return: 11 (the only odd number)

[160, 3, 1719, 19, 11, 13, -21] Should return: 160 (the only even number)

Problem: I can find the target number. It is "6" in this instance. But when i try to write it, it is written 3 times. I did not understand why it is not written once.


namespace sayitoplamları
{
  class Program
  {
      static void Main(string[] args)
      {
          int[] integers;
          integers = new int[] {1,3,5,6};
          int even = 0;
          int odd = 0;

          foreach (var num in integers)
          {
              if (num%2==0)
              {
                  even  = 1;
              }
              else
              {
                  odd  = 1;
              }

              if (even>1)
              {
                  foreach (var item in integers)
                  {
                      if (item%2!=0)
                      {
                          Console.WriteLine(item);
                      }

                  }
              }
              else if (odd>1)
              {
                  foreach (var item in integers)
                  {
                      if (item%2==0)
                      {
                          Console.WriteLine(item);
                      }
                  }
              }
          }
          
          


      }
  }
}```

CodePudding user response:

Since we are doing this assignment for a good grade :)

We really don't need to count both odd and even elements and can just count odd once as it more convenient - just add up all remainders ( odd % 2 = 1, even % 2 = 0). If we would need number of even elements it would be array.Length - countOdd.

int countOdd = 0;
for (int i = 0; i < array.Length; i  ) // or foreach
{
    countOdd  = array[i] % 2;
}

If you feel that trick with counting is too complicated and only needed for A mark on the assignment than variant of your original code is fine:

int countOdd = 0;
foreach (int element in array)
{
    if (element % 2 == 1)
        countOdd   ; // which is exactly countOdd  = 1, just shorter.
}

Now for the second part - finding the outlier. We don't even need to know if it is Odd or Even but rather just reminder from "% 2". We now know how many odd element - which could be either 1 (than desired remainder is 1 - we are looking for odd element) or more than one (then desired remainder is 0 as we are looking for even one).

int desiredRemainder = countOdd == 1 ? 1 : 0;

Now all its left is to check which element has desired remainder and return from our method:

foreach (int element in array)
{
   if (element % 2 == desiredRemainder)
   {
       return element; 
   }
}

Please note that assignment asks for "return" and not "print". Printing the value does not allow it to be used by the caller of the method unlike returning it. Using early return also saves some time to iterate the rest of array.

Complete code:

int FindOutlier(int[] array)
{
    int desiredReminder = array.Sum(x => x % 2) == 1 ? 1 : 0;
    return array.First(x => x % 2 == desiredReminder);
}

Alternative solution would be to iterate array only once - since we need to return first odd or first even number as soon as we figure out which one repeats similar how your tried with nested foreach. Notes since we are guaranteed that outlier present and is only one we don't need to try to remember first number - just current is ok.

int FindOutlier(int[] array)
{
   int? odd = null; // you can use separate int   bool instead
   int? even = null; 
   int countOdd = 0;
   int countEven = 0;
   foreach (int element in array)
   {
      bool isEven = element % 2 == 0;
      if (isEven)
      {
          countEven  ;
          even = element;
      }  
      else
      {
          countOdd  ;
          odd = element;
      }
      
      if (countEven > 1 && odd.HasValue)
           return odd.Value; 
      if (countOdd > 1 && even.HasValue) 
           return even.Value; 
   }
   throw new Exception("Value must be found before");
}

And if you ok to always iterate whole list code will be simple as again we'd need to only keep track of count of odd numbers (or even, just one kind) and return would not need to check if we found the value yet as we know that both type of numbers are present in the list:

int FindOutlier(int[] array)
{
   int odd = 0; 
   int even = 0; 
   int countEven = 0;
   foreach (int element in array)
   {
      if (element % 2 == 0)
      {
          countEven  ;
          even= element;
      }  
      else
      {
          odd = element;
      }
   }
   return countEven > 1 ? odd : even;
}

CodePudding user response:

You are mixing everything into a single algorithm. In most cases it's a better idea to have separate steps. In your case, the steps could be:

  1. Count odds and evens
  2. Find the outlier
  3. Print the result

That way you can make sure that the output does not interfere with the loops.

  class Program
  {
      static void Main(string[] args)
      {
          int[] integers;
          integers = new int[] {1,3,5,6};
          int even = 0;
          int odd = 0;
          
          // Count odds and evens
          foreach (var num in integers)
          {
              if (num%2==0)
              {
                  even  = 1;
              }
              else
              {
                  odd  = 1;
              }
         }


        // Find the outlier
        var outlier = 0;
        foreach (var item in integers)
      {
          if (even == 1 && item%2==0)
          {
            outlier = item;
            break;
          }
          if (odd == 1 && item%2!=0)
          {
            outlier = item;
            break;
            }
      }
      
      // Print the result
      Console.WriteLine(outlier);
      }
  }

You could even make this separate methods. It also makes sense due to SoC (separation of concerns) and if you want to achieve testability (unit tests).

You want a method anyway and use the return statement, as mentioned in the assignment

Write a method that takes the array as an argument and returns this "outlier" N.

Maybe you want to check the following code, which uses separate methods:

class Program
    {
        static void Main(string[] args)
        {
            int[] integers;
            integers = new int[] { 1, 3, 5, 6 };
            var outlier = FindOutlier(integers);
            Console.WriteLine(outlier);
        }

        private static int FindOutlier(int[] integers)
        {
            if (integers.Length < 3) throw new ArgumentException("Need at least 3 elements.");
            bool isEvenOutlier = IsEvenOutlier(integers);
            var outlier = FindOutlier(integers, isEvenOutlier ? 0:1);
            return outlier;
        }

        static bool IsEvenOutlier(int[] integers)
        {
            // Count odds and evens
            int even = 0;
            int odd = 0;
            foreach (var num in integers)
            {
                if (num % 2 == 0)
                {
                    even  = 1;
                }
                else
                {
                    odd  = 1;
                }
            }

            // Check which one occurs only once
            if (even == 1) return true;
            if (odd == 1) return false;
            throw new ArgumentException("No outlier in data.", nameof(integers));
        }

        private static int FindOutlier(int[] integers, int remainder)
        {
            foreach (var item in integers)
            {
                if (item % 2 == remainder)
                {
                    return item;
                }
            }

            throw new ArgumentException("No outlier in argument.", nameof(integers));
        }
    }

CodePudding user response:

The reason it is printing '6' 3 times is because you iterate over the inetegers inside a loop where you iterate over the integers. Once with num and again with item.

Consider what happens when you run it against [1,3,5,6].

  • num=1, you get odd=1 and even=0. Neither of the if conditions are true so we move to the next iteration.
  • num=3, you get odd=2 and even=0. odd > 1 so we iterate over the integers (with item) again and print '6' when item=6.
  • num=5, you get odd=3 and even=0. Again, this will print '6' when item=6.
  • num=6, you get odd=3 and even=1. Even though we incremented the even count, we still have odd > 3 so will print '6' when item=6.

It is best to iterate over the values once and store the last even and last odd numbers along with the count (it is more performant as well).

You can then print the last odd number if even > 0 (as there will only be one odd number, which will be the last one) or the last even number if odd > 0.

I.e.

namespace sayitoplamları
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] integers;
            integers = new int[] { 1, 3, 5, 6 };
            int even = 0;
            int? lastEven = null;
            int odd = 0;
            int? lastOdd = null;

            foreach (var num in integers)
            {
                if (num % 2 == 0)
                {
                    even  = 1;
                    lastEven = num;
                }
                else
                {
                    odd  = 1;
                    lastOdd = num;
                }
            }

            if (even > 1)
            {
                Console.WriteLine(lastOdd);
            }
            else if (odd > 1)
            {
                Console.WriteLine(lastEven);
            }
        }
    }
}

CodePudding user response:

because you didn't close the the first foreach loop before printing...

   namespace sayitoplamları
    {
      class Program
      {
          static void Main(string[] args)
          {
              int[] integers;
              integers = new int[] {1,3,5,6};
              int even = 0;
              int odd = 0;
    
              foreach (var num in integers)
              {
                  if (num%2==0)
                  {
                      even  = 1;
                  }
                  else
                  {
                      odd  = 1;
                  }//you need to close it here
    
                  if (even>1)
                  {
                      foreach (var item in integers)
                      {
                          if (item%2!=0)
                          {
                              Console.WriteLine(item);
                          }
    
                      }
                  }
                  else if (odd>1)
                  {
                      foreach (var item in integers)
                      {
                          if (item%2==0)
                          {
                              Console.WriteLine(item);
                          }
                      }
                  }
              }
              
              
    
    
          }//not here
      }
    }
  • Related