Skip to main content
square brackets instead of curly
Source Link

My code returns the correct answer the first duplicate is 3 (represented as int f) . I am struggling finding a more efficient way to find the first duplicate?

My constraints are \$ 1 ≤ a.length ≤ 10^5, 1 ≤ a[i] ≤ a.length \$ .

class Program
{
  static void Main(string[] args)
  {
    int[] a = [2{2, 3, 3, 1, 5, 2];2};

    int f = FirstDuplicate(a);

    Console.ReadLine();
   }
public static int FirstDuplicate(int[] a)
{

    int[] answer = new int[2];
    answer[0] = -1;
    answer[1] = a.Length;
    for (int i = 0; i < a.Length - 1; i++)
        for (int j = i; j < a.Length; j++)
            if (a[i] == a[j] && i != j)
            {
                if (i < answer[1])
                {
                    answer[0] = a[i];
                    answer[1] = j;
                    break;

                }
            }

    return answer[0];
  }
}

My code returns the correct answer the first duplicate is 3 (represented as int f) . I am struggling finding a more efficient way to find the first duplicate?

My constraints are \$ 1 ≤ a.length ≤ 10^5, 1 ≤ a[i] ≤ a.length \$ .

class Program
{
  static void Main(string[] args)
  {
    int[] a = [2, 3, 3, 1, 5, 2];

    int f = FirstDuplicate(a);

    Console.ReadLine();
   }
public static int FirstDuplicate(int[] a)
{

    int[] answer = new int[2];
    answer[0] = -1;
    answer[1] = a.Length;
    for (int i = 0; i < a.Length - 1; i++)
        for (int j = i; j < a.Length; j++)
            if (a[i] == a[j] && i != j)
            {
                if (i < answer[1])
                {
                    answer[0] = a[i];
                    answer[1] = j;
                    break;

                }
            }

    return answer[0];
  }
}

My code returns the correct answer the first duplicate is 3 (represented as int f) . I am struggling finding a more efficient way to find the first duplicate?

My constraints are \$ 1 ≤ a.length ≤ 10^5, 1 ≤ a[i] ≤ a.length \$ .

class Program
{
  static void Main(string[] args)
  {
    int[] a = {2, 3, 3, 1, 5, 2};

    int f = FirstDuplicate(a);

    Console.ReadLine();
   }
public static int FirstDuplicate(int[] a)
{

    int[] answer = new int[2];
    answer[0] = -1;
    answer[1] = a.Length;
    for (int i = 0; i < a.Length - 1; i++)
        for (int j = i; j < a.Length; j++)
            if (a[i] == a[j] && i != j)
            {
                if (i < answer[1])
                {
                    answer[0] = a[i];
                    answer[1] = j;
                    break;

                }
            }

    return answer[0];
  }
}
edited title and tags
Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191

Improving performance when finding duplicates Finding the first duplicate in an array

Sorry, had the wrong array set placed with the answer.
Source Link

My code returns the correct answer the first duplicate is 3 (represented as int f) . I am struggling finding a more efficient way to find the first duplicate?

My constraints are \$ 1 ≤ a.length ≤ 10^5, 1 ≤ a[i] ≤ a.length \$ .

class Program
{
  static void Main(string[] args)
  {
    int[] a = { 1[2, 23, 3, 41, 5, 6 };2];

    int f = FirstDuplicate(a);

    Console.ReadLine();
   }
public static int FirstDuplicate(int[] a)
{

    int[] answer = new int[2];
    answer[0] = -1;
    answer[1] = a.Length;
    for (int i = 0; i < a.Length - 1; i++)
        for (int j = i; j < a.Length; j++)
            if (a[i] == a[j] && i != j)
            {
                if (i < answer[1])
                {
                    answer[0] = a[i];
                    answer[1] = j;
                    break;

                }
            }

    return answer[0];
  }
}

My code returns the correct answer the first duplicate is 3 (represented as int f) . I am struggling finding a more efficient way to find the first duplicate?

My constraints are \$ 1 ≤ a.length ≤ 10^5, 1 ≤ a[i] ≤ a.length \$ .

class Program
{
  static void Main(string[] args)
  {
    int[] a = { 1, 2, 3, 4, 5, 6 };

    int f = FirstDuplicate(a);

    Console.ReadLine();
   }
public static int FirstDuplicate(int[] a)
{

    int[] answer = new int[2];
    answer[0] = -1;
    answer[1] = a.Length;
    for (int i = 0; i < a.Length - 1; i++)
        for (int j = i; j < a.Length; j++)
            if (a[i] == a[j] && i != j)
            {
                if (i < answer[1])
                {
                    answer[0] = a[i];
                    answer[1] = j;
                    break;

                }
            }

    return answer[0];
  }
}

My code returns the correct answer the first duplicate is 3 (represented as int f) . I am struggling finding a more efficient way to find the first duplicate?

My constraints are \$ 1 ≤ a.length ≤ 10^5, 1 ≤ a[i] ≤ a.length \$ .

class Program
{
  static void Main(string[] args)
  {
    int[] a = [2, 3, 3, 1, 5, 2];

    int f = FirstDuplicate(a);

    Console.ReadLine();
   }
public static int FirstDuplicate(int[] a)
{

    int[] answer = new int[2];
    answer[0] = -1;
    answer[1] = a.Length;
    for (int i = 0; i < a.Length - 1; i++)
        for (int j = i; j < a.Length; j++)
            if (a[i] == a[j] && i != j)
            {
                if (i < answer[1])
                {
                    answer[0] = a[i];
                    answer[1] = j;
                    break;

                }
            }

    return answer[0];
  }
}
edited body
Source Link
Loading
Tweeted twitter.com/StackCodeReview/status/992213289001193472
use inline code span for code within text
Source Link
Loading
Source Link
Loading