Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Israt Zahan Sathi
Israt Zahan Sathi

Posted on

     

Find Duplicate in an Array in C++

Problem 1
You will be given an arrayarr of sizen. Print "YES" if there is any duplicate value in the array, "NO" otherwise.

Sample Input 1
5
2 8 5 7 3
Sample Output 1
NO

Sample Input 2
7
2 1 3 5 2 1 9
Sample Output 2
YES

Input Format

  • First line will containn.
  • Second line will contain the arrayarr.Image description

Follow the steps to resolve the problem:

  • Assuming we get an input.
  • Now declaren and input its value.
  • Declare an array name isarr, and take that as input.
  • Take atmp _to check if there are duplicate values in the array.[_tmp _is a variable used as a flag to indicate whether there are duplicate elements in the array. The name "_tmp" is often used as a shorthand for "temporary" or "temporary variable." Its purpose is to store a temporary state or result during the execution of the program.]
  • Then loop through and look for duplicates. If duplicate value is found then I will set"tmp = 1" and also break the loop.
  • After that I will check, if duplicate value is found I will print"YES", if not found I will print"NO".

Below is the implementation of the above method:

#include <bits/stdc++.h>using namespace std;int main(){    int n;    cin >> n;    int arr[n];    for (int i = 0; i < n; i++)        cin >> arr[i];    int tmp = 0;    for (int i = 0; i < n; i++)    {        for (int j = i + 1; j < n; j++)        {            if (arr[i] == arr[j])            {                tmp = 1;                break;            }        }    }    if (tmp)        cout << "YES" << endl;    else        cout << "NO" << endl;    return 0;}
Enter fullscreen modeExit fullscreen mode

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
pauljlucas profile image
Paul J. Lucas
C++ Jedi Master
  • Email
  • Location
    San Francisco Bay Area
  • Education
    M.S., University of Illinois at Urbana-Champaign
  • Work
    Retired Principal Software Engineer
  • Joined

Headers in thebits directory arenot meant to be included by users directly. You should#include the headers you need explicitly.

CollapseExpand
 
ijsathi profile image
Israt Zahan Sathi
Looking for something new💜
  • Location
    Dhaka 1207, Bangladesh
  • Education
    Dhaka Mohila Polytechnic Institute, Bangladesh
  • Work
    Student
  • Joined

#include <bits/stdc++.h> is all in one. So I used it.
But why users cannot directly use it??

CollapseExpand
 
pauljlucas profile image
Paul J. Lucas
C++ Jedi Master
  • Email
  • Location
    San Francisco Bay Area
  • Education
    M.S., University of Illinois at Urbana-Champaign
  • Work
    Retired Principal Software Engineer
  • Joined

It’s not standard. It’s specific to GNU. It’s not guaranteed to exist anywhere else.

That aside, having the compiler read and parse every single standard header file would greatly increase the compile time for non-trivial programs.

Thread Thread
 
ijsathi profile image
Israt Zahan Sathi
Looking for something new💜
  • Location
    Dhaka 1207, Bangladesh
  • Education
    Dhaka Mohila Polytechnic Institute, Bangladesh
  • Work
    Student
  • Joined

Many thanks for the valuable information💛

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Looking for something new💜
  • Location
    Dhaka 1207, Bangladesh
  • Education
    Dhaka Mohila Polytechnic Institute, Bangladesh
  • Work
    Student
  • Joined

More fromIsrat Zahan Sathi

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp