Javatpoint Logo
Javatpoint Logo

4. C program to print the duplicate elements of an array

In this program, we need to print the duplicate elements present in the array. This can be done through two loops. The first loop will select an element and the second loop will iteration through the array by comparing the selected element with other elements. If a match is found, print the duplicate element.

C program to print the duplicate elements of an array

In the above array, the first duplicate will be found at the index 4 which is the duplicate of the element (2) present at index 1. So, duplicate elements in the above array are 2, 3 and 8.

ALGORITHM:

  • STEP 1: START
  • STEP 2: INITIALIZE arr[]= {1, 2, 3, 4, 2, 7, 8, 8, 3}.
  • STEP 3: length = sizeof(arr)/sizeof(arr[0])
  • STEP 4: PRINT "Duplicate elements in given array:"
  • STEP 5: SET i=0. REPEAT STEP 6 to STEP 9 UNTIL i<length
  • STEP 6: SET j=i+1. REPEAT STEP 7 and STEP 8 UNTIL i<length
  • STEP 7: if(arr[i] == arr[j])
                  PRINT arr[j]
  • STEP 8: j=j+1
  • STEP 9: i+1.
  • STEP 10: RETURN 0.
  • STEP 11: END

PROGRAM:

Output:

Duplicate elements in given array:
2
3
8
Next TopicC Programs





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA