How To Detect Duplicate Values In Primitive Java Array? - Tutorialspoint

  • Home
  • Whiteboard
  • Online Compilers
  • Practice
  • Articles
  • AI Assistant
  • Jobs
  • Tools
  • Corporate Training
  • Courses
  • Certifications
Menu Categories Login
  • Switch theme
  • SQL
  • HTML
  • CSS
  • Javascript
  • Python
  • Java
  • C
  • C++
  • PHP
  • Scala
  • C#
  • Tailwind CSS
  • Node.js
  • MySQL
  • MongoDB
  • PL/SQL
  • Swift
  • Bootstrap
  • R
  • Machine Learning
  • Blockchain
  • Angular
  • React Native
  • Computer Fundamentals
  • Compiler Design
  • Operating System
  • Data Structure and Algorithms
  • Computer Network
  • DBMS
  • Excel
Technical Questions and Answers
  • Data Structure Data Structure
  • Networking Networking
  • RDBMS RDBMS
  • Operating System Operating System
  • Java Java
  • MS Excel MS Excel
  • iOS iOS
  • HTML HTML
  • CSS CSS
  • Android Android
  • Python Python
  • C Programming C Programming
  • C++ C++
  • C# C#
  • MongoDB MongoDB
  • MySQL MySQL
  • Javascript Javascript
  • PHP PHP
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who
How to detect duplicate values in primitive Java array? JavaObject Oriented ProgrammingProgramming

To detect the duplicate values in an array you need to compare each element of the array to all the remaining elements, in case of a match you got your duplicate element.

One solution to do so you need to use two loops (nested) where the inner loop starts with i+1 (where i is the variable of outer loop) to avoid repetitions in comparison.

Example

import java.util.Arrays; import java.util.Scanner; public class DetectDuplcate {        public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array that is to be created::");       int size = sc.nextInt();       int[] myArray = new int[size];       System.out.println("Enter the elements of the array ::");           for(int i=0; i<size; i++) {          myArray[i] = sc.nextInt();       }       System.out.println("The array created is ::"+Arrays.toString(myArray));       System.out.println("indices of the duplicate elements :: ");           for(int i=0; i<myArray.length; i++) {          for (int j=i+1; j<myArray.length; j++) {             if(myArray[i] == myArray[j]) {                System.out.println(j);             }          }       }    } }

Output

Enter the size of the array that is to be created :: 6 Enter the elements of the array :: 87 52 87 63 41 63 The array created is :: [87, 52, 87, 63, 41, 63] indices of the duplicate elements :: 2 5

Solution 2: In addition to this we have a more reliable solution:

The interface set does not allow duplicate elements, therefore, create a set object and try to add each element to it using the add() method in case of repetition of elements this method returns false:

Example

import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class DetectDuplcateUsingSet {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array that is to be created::");       int size = sc.nextInt();       int[] myArray = new int[size];       System.out.println("Enter the elements of the array ::");       for(int i=0; i<size; i++) {          myArray[i] = sc.nextInt();       }       System.out.println("The array created is ::"+Arrays.toString(myArray));       System.out.println("indices of duplicate elements in the array are elements::");       Set set = new HashSet();         for(int i=0; i<myArray.length; i++) {          if(!set.add(myArray[i])) {             System.out.println(i);          }       }    } }

Output

Enter the size of the array that is to be created :: 5 Enter the elements of the array :: 78 56 23 78 45 The array created is :: [78, 56, 23, 78, 45] indices of duplicate elements in the array are elements:: 3 karthikeya Boyini karthikeya Boyini Updated on: 2019-12-19T10:20:51+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started Print Page Previous Next Advertisements

Tag » How To Find Duplicate Number In Array Java