Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7996123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:37:33+00:00 2026-06-04T14:37:33+00:00

I just want to write code for comparing a 1D array with a 2D

  • 0

I just want to write code for comparing a 1D array with a 2D array… I am working on writing a compiler and want to compare a 1D array which contains my code and other 2D array in which I have made a symbol table.
I have written code, but it’s not working.

for (int x = 0; x < symboltable1.Length; x++)
{
    for (int y = 0; y < symboltable1.Length; y++)
    {
        for (int z = 0; z < text.Length; z++)
        {
            if (symboltable1[x,y] == text[z])
                listBox2.Items.Add(text[z]);
            else
                MessageBox.Show("poor");
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:37:35+00:00Added an answer on June 4, 2026 at 2:37 pm

    Your method is too slow O(n*m*l) (where l is the length of text,n & m the dimensions of your symboltable.)

    Transforming your symboltable into a sorted indexed table would give you an O(l*log(n*m)), and using a hashtable instead would give you an almost O(l).

    I’ve implemented the three approach just for fun:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace test
    {
        class Program
        {
            class IndexElement:IComparable<IndexElement>
            {
                public string value;
                public int x;
                public int y;
                public IndexElement(string v,int x,int y)
                {
                    this.value = v;
                    this.x = x;
                    this.y = y;
                }
    
                public int CompareTo(IndexElement other)
                {
                    return value.CompareTo(other.value);
                }
            }
    
            struct Position
            {
                public int x;
                public int y;
                public Position(int x, int y)
                {
                    this.x = x;
                    this.y = y;
                }
            }
            static void Main(string[] args)
            {
                string[,] symbols = new string[,] { { "if", "else" }, { "for", "foreach" }, { "while", "do" } };
                string[] text = new string[] { "for", "int", "in", "if", "then" };
                Dictionary<string, Position> dictionary = BuildDict(symbols);
                IndexElement[] index = BuildIndex(symbols);
                Console.WriteLine("Brute:");
                foreach (string s in CompareUsingBrute(text, symbols))
                {
                    Console.WriteLine(s);
                }
                Console.WriteLine("Dict:");
                foreach (string s in CompareUsingIndex(text, dictionary))
                {
                    Console.WriteLine(s);
                }
                Console.WriteLine("Indexing:");
                foreach (string s in CompareUsingDictionary(text, index))
                {
                    Console.WriteLine(s);
                }
                Console.ReadKey();
            }
    
            private static List<string> CompareUsingBrute(string[] text, string[,] symbols)
            {
                List<string> res = new List<string>();
                for (int x = 0; x < symbols.GetLength(0); x++)
                {
                    for (int y = 0; y < symbols.GetLength(1); y++)
                    {
                        for (int z = 0; z < text.Length; z++)
                        {
                            if (symbols[x, y] == text[z])
                                res.Add(text[z]);                        
                        }
                    }
                }
                return res;
    
            }
    
            private static List<string> CompareUsingDictionary(string[] text, IndexElement[] index)
            {
                List<string> res = new List<string>();
                foreach (string s in text)
                {
                    if (Array.BinarySearch<IndexElement>(index, new IndexElement(s, 0, 0)) >= 0)
                    {
                        res.Add(s);
                    }
                }
                return res;
            }
    
            private static IndexElement[] BuildIndex(string[,] symbols)
            {
                IndexElement[] res = new IndexElement[symbols.Length];
                int index = 0;
                for (int i = 0; i < symbols.GetLength(0); i++)
                {
                    for (int j = 0; j < symbols.GetLength(1); j++)
                    {
                        res[index] = new IndexElement(symbols[i, j], i, j);
                        index++;
                    }
    
                }
                Array.Sort(res);
                return res;
            }
    
            private static List<string> CompareUsingIndex(string[] text, Dictionary<string, Position> dictionary)
            {
                List<string> res = new List<string>();
                foreach (string s in text)
                {
                    Position p;
                    if (dictionary.TryGetValue(s,out p))
                    {
                        res.Add(s);
                    }
                }
                return res;
            }
    
            private static Dictionary<string, Position> BuildDict(string[,] symbols)
            {
                Dictionary<string, Position> res = new Dictionary<string, Position>();
                for (int i = 0; i < symbols.GetLength(0); i++)
                {
                    for (int j = 0; j < symbols.GetLength(1); j++)
                    {
                        res.Add(symbols[i, j], new Position(i, j));
                    }
                }
                return res;
            }
    
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I write a lot of Python code where I just want to read a
i just join phonegap, i want a built test application. i write some code
I just want to write code inside my assembly to detect whether it is
Extremely just-started-yesterday new to F#. What I want: To write code that parses the
I just want too write simple .asm code for TASM that work as for
just think that when I opened my file then when I want to write
Basically I want to write php code that lists all the contents that are
I am writing a piece of code which is going to order the list
I just want to read (and maybe write) UTF-8 data. haskell.org still advertises System.Streams
I simply want to write some code that makes use of recursion of functions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.