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

  • Home
  • SEARCH
  • 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 461475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:58:42+00:00 2026-05-12T22:58:42+00:00

Given two arrays as parameters (x and y) and find the starting index where

  • 0

Given two arrays as parameters (x and y) and find the starting index where the first occurrence of y in x. I am wondering what the simplest or the fastest implementation would be.

Example:

when x = {1,2,4,2,3,4,5,6}
     y =       {2,3}
result
     starting index should be 3

Update: Since my code is wrong I removed it from the question.

  • 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-05-12T22:58:42+00:00Added an answer on May 12, 2026 at 10:58 pm

    Here is a simple (yet fairly efficient) implementation that finds all occurances of the array, not just the first one:

    static class ArrayExtensions {
    
      public static IEnumerable<int> StartingIndex(this int[] x, int[] y) {
        IEnumerable<int> index = Enumerable.Range(0, x.Length - y.Length + 1);
        for (int i = 0; i < y.Length; i++) {
          index = index.Where(n => x[n + i] == y[i]).ToArray();
        }
        return index;
      }
    
    }
    

    Example:

    int[] x = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
    int[] y = { 2, 3 };
    foreach (int i in x.StartingIndex(y)) {
      Console.WriteLine(i);
    }
    

    Output:

    1
    5
    9
    

    The method first loops through the x array to find all occurances of the first item in the y array, and place the index of those in the index array. Then it goes on to reduce the matches by checking which of those also match the second item in the y array. When all items in the y array is checked, the index array contains only the full matches.

    Edit:
    An alternative implementation would be to remove the ToArray call from the statement in the loop, making it just:

    index = index.Where(n => x[n + i] == y[i]);
    

    This would totally change how the method works. Instead of looping through the items level by level, it would return an enumerator with nested expressions, deferring the search to the time when the enumerator was iterated. That means that you could get only the first match if you wanted:

    int index = x.StartingIndex(y).First();
    

    This would not find all matches and then return the first, it would just search until the first was found and then return it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given two arrays, how to find the maximum element which is common to both
Given two arrays A[n] and B[m] , how can I find the smallest window
Given two arrays, A and B, i want to find a number from A
Given two sorted arrays of integers, a and b , and an integer c
Given two sorted arrays: A and B . The size of array A is
Given two struct arrays A and B with field f1: A = struct('f1',{1,2,3}) B
I've got two two-dimensional arrays and I need to filter the first array's rows
The exercise says Make a function with parameters two int arrays and k which
Given two arrays of integers, B and A, how can we rearrange their elements
Given two data frames: C1<-c(3,4,4,4,5) C2<-c(3,7,3,4,5) C3<-c(5,6,3,7,4) DF<-data.frame(C1=C1,C2=C2,C3=C3) DF C1 C2 C3 1 3

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.