Contains Duplicate in java

Contains Duplicate in java

Given an array ‘arr’ with ‘n’ positive integers. Find if the array contains any duplicates.

Input Format

The first line of input contains the integer n.

The last line contains n spaced integers.

Output Format

For each test case print “true” without quotes if duplicates are present or print “false” without quotes if duplicates are not present.

Example 1

Input

4
1 2 3 1

Output

true

Explanation

1 is duplicate here.

Example 2

Input

4
1 2 3 4

Output

false

Explanation

No duplicate present.

Constraints

1 <= n <= 10000

0 <= arr[i] <= 100000

Solution of Contains Duplicate in java:–

import java.util.; import java.lang.;
import java.io.*;

public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();

  int arr[] = new int[N];
  Boolean demo = false;

  for(int i=0;i<N;i++)
    {
      arr[i]=sc.nextInt();
    }

  for(int i=0;i<N;i++)
    {
      for(int j=0; j<N;j++)
        {
          if(i!=j)
          {
            if(arr[i]==arr[j])
            {
              demo=true;
              break;
            }
          }
        }
    }
  if(demo==true)
  {
    System.out.println("true");
  }
  else{
    System.out.println("false");
  }
}
}

Add a Comment

Your email address will not be published. Required fields are marked *