Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Efficent method to find index of a value in a array

Efficent method to find index of a value in a array

Scheduled Pinned Locked Moved C#
tutorialcsharpdatabaselinqdata-structures
7 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PozzaVecia
    wrote on last edited by
    #1

    Need to do the following: given a array 'a' of double of monotone increasing values and a given value I have to find the index in the array so that V a.Max() or V < a.Min() the return 100.

    double[] a = new double[]{1,2,3,4}

    I look for a function F so that for example (implementing whaat described above: F[0.5] =100; F[1] = 0; F[2] = 0; F[2.1] =1; F[4] =3; F[4.1] =100; It can be done with for...loop of course. Is it efficent or it is better using LINQ or Array.FindIndex or...smarter solution? (if it is more efficent LINQ or Array.FindIndex, how to do it?)

    P B Richard DeemingR V A 5 Replies Last reply
    0
    • P PozzaVecia

      Need to do the following: given a array 'a' of double of monotone increasing values and a given value I have to find the index in the array so that V a.Max() or V < a.Min() the return 100.

      double[] a = new double[]{1,2,3,4}

      I look for a function F so that for example (implementing whaat described above: F[0.5] =100; F[1] = 0; F[2] = 0; F[2.1] =1; F[4] =3; F[4.1] =100; It can be done with for...loop of course. Is it efficent or it is better using LINQ or Array.FindIndex or...smarter solution? (if it is more efficent LINQ or Array.FindIndex, how to do it?)

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      TheGermoz wrote:

      is better using LINQ

      No. Probably not anyway, but I'm unclear what you are trying to do.

      1 Reply Last reply
      0
      • P PozzaVecia

        Need to do the following: given a array 'a' of double of monotone increasing values and a given value I have to find the index in the array so that V a.Max() or V < a.Min() the return 100.

        double[] a = new double[]{1,2,3,4}

        I look for a function F so that for example (implementing whaat described above: F[0.5] =100; F[1] = 0; F[2] = 0; F[2.1] =1; F[4] =3; F[4.1] =100; It can be done with for...loop of course. Is it efficent or it is better using LINQ or Array.FindIndex or...smarter solution? (if it is more efficent LINQ or Array.FindIndex, how to do it?)

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #3

        100 is a stupid failure value. What if you have a 100 element array? -1 is traditional for 'not found' (see Array.IndexOf and the like). I think your expected output is wrong ... if F[4] is 3 then F[2] must be 1. Does

        int F(double[] arr, int value){
        return -1 + Array.FindIndex(arr, d => d >= value);
        }

        ... do what you want?

        P 1 Reply Last reply
        0
        • P PozzaVecia

          Need to do the following: given a array 'a' of double of monotone increasing values and a given value I have to find the index in the array so that V a.Max() or V < a.Min() the return 100.

          double[] a = new double[]{1,2,3,4}

          I look for a function F so that for example (implementing whaat described above: F[0.5] =100; F[1] = 0; F[2] = 0; F[2.1] =1; F[4] =3; F[4.1] =100; It can be done with for...loop of course. Is it efficent or it is better using LINQ or Array.FindIndex or...smarter solution? (if it is more efficent LINQ or Array.FindIndex, how to do it?)

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          If the values are monotonically increasing, Array.BinarySearch[^] will probably be the most efficient way to find a particular value.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          • P PozzaVecia

            Need to do the following: given a array 'a' of double of monotone increasing values and a given value I have to find the index in the array so that V a.Max() or V < a.Min() the return 100.

            double[] a = new double[]{1,2,3,4}

            I look for a function F so that for example (implementing whaat described above: F[0.5] =100; F[1] = 0; F[2] = 0; F[2.1] =1; F[4] =3; F[4.1] =100; It can be done with for...loop of course. Is it efficent or it is better using LINQ or Array.FindIndex or...smarter solution? (if it is more efficent LINQ or Array.FindIndex, how to do it?)

            V Offline
            V Offline
            V 0
            wrote on last edited by
            #5

            I would put it in a List. That contains a functions to search. See here[^].

            List a = new List();
            a.Add(1);
            a.Add(2);
            a.Add(3);
            a.Add(4);

            //then you can inplement the BinarySearch eg. or use the Find methods.

            Hope this helps.

            V.
            (MQOTD Rules and previous Solutions )

            1 Reply Last reply
            0
            • P PozzaVecia

              Need to do the following: given a array 'a' of double of monotone increasing values and a given value I have to find the index in the array so that V a.Max() or V < a.Min() the return 100.

              double[] a = new double[]{1,2,3,4}

              I look for a function F so that for example (implementing whaat described above: F[0.5] =100; F[1] = 0; F[2] = 0; F[2.1] =1; F[4] =3; F[4.1] =100; It can be done with for...loop of course. Is it efficent or it is better using LINQ or Array.FindIndex or...smarter solution? (if it is more efficent LINQ or Array.FindIndex, how to do it?)

              A Offline
              A Offline
              Abhinav S
              wrote on last edited by
              #6

              TheGermoz wrote:

              It can be done with for...loop of course. Is it efficent or it is better using LINQ or Array.FindIndex or...smarter solution? (if it is more efficent LINQ or Array.FindIndex, how to do it?)

              The difference in performance would only just be better for LINQ. If your function is not many to one, you could directly use a key value pair collection.

              WP7.5 Apps - XKCD | Calvin | SMBC | Sound Meter | Speed Dial

              1 Reply Last reply
              0
              • B BobJanova

                100 is a stupid failure value. What if you have a 100 element array? -1 is traditional for 'not found' (see Array.IndexOf and the like). I think your expected output is wrong ... if F[4] is 3 then F[2] must be 1. Does

                int F(double[] arr, int value){
                return -1 + Array.FindIndex(arr, d => d >= value);
                }

                ... do what you want?

                P Offline
                P Offline
                PozzaVecia
                wrote on last edited by
                #7

                yes sorry my error F[2] must be 1. Yes is solve it. The only error is for F[1] should be 0 and not -1. Is it more efficient Linq or BinarySearch?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups