简体   繁体   中英

comparing two arrays with only loops

I'm stuck with arrays and need some help.

I have two arrays and I'm comparing it using foreach loop. I want the program to write "you won" ONLY if there are two numbers in winningNumbers that matches with someNumbers . If there is only one match, the output should be "You lost"

(I have to use loops to solve this problem)

int[] someNumbers = { 1, 2, 3, 4, 5, 6 };
int[] winningNumbers = { 1, 2, 13, 14, 15, 16 };
bool winning = false;

foreach (var item1 in someNumbers)
{
    foreach (var item2 in winningNumbers)
    {
        if (item1 == item2)
        {
            winning = true;
        }
    }
}

if (winning == true )
{
    Console.WriteLine("You won");
}

else
{
    Console.WriteLine("You lost");
}

Use a counter for each match. If it's >= 2 then it reports a win. If you require it to be exactly two matches, remove the > operator. Try it here: https://dotnetfiddle.net/sjYbYk

edit: stringbuilder added to report winning matches.

using System;
using System.Text;
                    
public class Program
{
    public static void Main()
    {
            int[] someNumbers = { 1, 2, 3, 4, 5, 6 };
            int[] winningNumbers = { 1, 2, 13, 14, 15, 16 };
            int matched = 0;
            StringBuilder sb = new StringBuilder();
            sb.Append("You Matched ");
            foreach (var item1 in someNumbers)
            {
                foreach (var item2 in winningNumbers)
                {
                    if (item1 == item2)
                    {
                        matched++;
                        sb.Append(item1 + ",");
                    }
                }
            }

            if (matched >= 2 )
            {
                Console.WriteLine("You won");
                Console.WriteLine(sb.ToString().TrimEnd(','));
            }

            else
            {
                Console.WriteLine("You lost");
            }
    }
}

Since you're looping through each array you could just increment a counter every time you encounter a match.

            int[] someNumbers = { 1, 2, 3, 4, 5, 6 };
            int[] winningNumbers = { 1, 2, 13, 14, 15, 16 };
            int matches = 0;

            foreach (var item1 in someNumbers)
            {
                foreach (var item2 in winningNumbers)
                {
                    if (item1 == item2)
                    {
                        matches++;
                    }
                }
            }

            if (matches == 2 )
            {
                Console.WriteLine("You won");
            }

            else
            {
                Console.WriteLine("You lost");
            }

I realized that both your array in sorted ascending order. If that is the case I would prefer the approach of two pointers to yeild better performance.

Time Complexity - O(m+n): where m,n are lengths of your both arrays.

In case if you don't have sorted array you can go with brute force approach as mentioned in other answers or sort the arrays first and then go with below approach.

Time complexity of brute force - O(m*n)

Time complexity of sort and two pointer approach - O(mLogm + nLogn)

int[] someNumbers = { 1, 2, 3, 4, 5, 6 };
int[] winningNumbers = { 1, 2, 13, 14, 15, 16 };
int matches = 0;
int i=0,j=0;
while(i<someNumbers.Length && j<winningNumbers.Length)
{
   if(someNumbers[i]==winningNumbers[j])
   {
       matches++;
       i++;
       j++;
   }
   else if(someNumbers[i]<winningNumbers[j])
   {
       i++;
   }
   else if(someNumbers[i]>winningNumbers[j])
   {
       j++;
   }
}

if (matches >=2 )
{
    Console.WriteLine("You won");
 }
 else
 {
     Console.WriteLine("You lost");
 }

According to your code, only one case will be printed. save the results in a new array and then print them.

        int[] someNumbers = { 1, 2, 3, 4, 5, 6 };
        int[] winningNumbers = { 1, 2, 13, 14, 15, 12};
        ArrayList compare_result = new ArrayList();
        //two loop to compare someNumbers and winningNumbers
        for (int x = 0; x < someNumbers.Length; x++)
        {
            for (int y = 0; y < winningNumbers.Length; y++)
            {
                if (someNumbers[x] == winningNumbers[y])
                {
                    compare_result.Add(someNumbers[x]);
                }
            }
        }
        //print comparing result
       if (compare_result.Count > 0)
        {
            Console.WriteLine("you win:");
            for (int i = 0; i < compare_result.Count; i++)
            {
                Console.WriteLine(compare_result[i]);
            }
        }
        else
        {
            Console.WriteLine("You lost");
        }
   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM