0

I want to be able to count multiple entries in an already sorted array. In previous attempts, I've tried to use a list like this.

public static List<int> duplicate = new List<int>();

However, it wouldn't count properly and kept printing the same index number. I'm pretty much stumped on what to do at this point any help would be appreciated.

class binarysearch
{
    public static int lowestIndex = 0;
    public static int highestIndex;
    public static int middleIndex = 0;
    public static int indexValue = -1;

    public static int binarySearch(double[] data, double target)
    {
        int highestIndex = data.Length - 1;

        while (lowestIndex < highestIndex)
        {
            middleIndex = lowestIndex + (highestIndex - lowestIndex) / 2;
            if (data[middleIndex] == target)
            {
                indexValue = middleIndex;
                highestIndex = middleIndex;
            }
            else if (data[middleIndex] > target)
            {
                highestIndex = middleIndex;
            }
            else
            {
                lowestIndex = middleIndex + 1;
            }   
        }

        return indexValue;
    }
}

edit

public static List<int> duplicate = new List<int>();

is declared earlier in the code in

class dataset
{
   public static List<int> duplicate = new List<int>();
}

then printed later in the main method

foreach (object dupes in dataset.duplicate)
{
 Console.WriteLine(dupes);
}
Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
  • What is `public static List duplicate = new List();`? It's not legal code unless you've defined your own `List` type. And how does it compute anything? Do you need to read [ask]? – Enigmativity May 03 '18 at 12:42
  • https://stackoverflow.com/questions/454601/how-to-count-duplicates-in-list-with-linq You can refer the above 1. All you need is tweak the code a bit to get the indexes of the duplicate numbers. – Bendram May 03 '18 at 12:43
  • @Enigmativity i've amended the question to clarify –  May 03 '18 at 12:52
  • 2
    If the array is sorted, you can check the next value if it equals to the previous one. – Rainbow May 03 '18 at 12:58
  • @Enigmativity because it wasn't formatted as code, the `` was swallowed as an unknown html tag – Hans Kesting May 03 '18 at 13:06
  • Warning: using static values for those indexes will lead to problems: 1) using this twice doesn't reset them (easily fixed); 2) multithreaded use will clash (and for instance a webapp is multithreaded) – Hans Kesting May 03 '18 at 13:11
  • I know, its static for testing purposes right now it would be changed in the final build of course –  May 03 '18 at 13:14
  • @user1672994 - How can you traverse an array in `O(1)`? It's `O(n)` by definition. – Enigmativity May 03 '18 at 13:21
  • @thunderheck - What does the`List duplicate` have to do with the `binarysearch` class? That is operating on a `double[]`. – Enigmativity May 03 '18 at 13:22
  • Yes, it should be `O(n)`. – user1672994 May 03 '18 at 13:22

0 Answers0