java - Find all repeated values -


I am writing a code to find duplicate values ​​that are inserted into an array. But I'm not able to get the right code. Does anyone suggest me with an immediate code, then (using array list)?

  package com.Test1.java; Import java.util.Arrays; Import java.util.Scanner; Public Class Test 1 {Personal Static Scanners; Public static zero main (string [] args) {int n; S = new scanner (System.in); System.out.println ("Enter the number of values ​​you want to enter:"); N = s.nextInt (); Int number [] = new int [n]; System.out.println ("Enter Value:"); (Int i = 1; i & lt; = number.length; i ++) for {number [i] = s.nextInt (); } System.out.println ("These values ​​have been entered:"); Arrays.sort (number); For (int j = 1; j & lt; = number.length; j ++) {if (number [j] == number [j-1]) {System.out.println ("is a duplicate number" Number [ja]); }}}}  

I am getting this error output:

  how many values ​​you want to enter Enter: 5 values: 1 2 2 3 4 Exceptions in thread "main": java.lang.ArrayIndexOutOfBoundsException: 5 com.Test1.java.Test1.main (Test1.java:17) **  

Try this approach:

  1. In an list Change .
  2. Turn on list in set to get each member once.
  3. Check if set is in multiple list .

Java code:

  import java.util .Arrays; Import java.util.Collections; Import java.util.HashSet; Import java.util.list; Import java.util.Set; Public class Soplag {Public Static Zero Main (string [] Args throws exceptions {integer number [] = new integer [] {1, 3, 4, 6, 7, 9, 0, 9, 8, 6, 5) , 4, 3, 2, 1}; & Lt; Integer & gt; A = arrays.asList (number); Set up & lt; Integer & gt; S = new hashसेट (A); For (integer i: s) {if (Collection Frequency (A, I)> 1) {System.out.println (i + "is a duplicate"); }}}}  

Output:

  1 duplicate 3 is duplicate 4 duplicate 6 duplicate 9 is a duplicate  
< / Div>

Comments