tag:blogger.com,1999:blog-8712770457197348465.post1594195688447010264..comments2024-03-29T05:54:46.190-07:00Comments on Javarevisited: How to Check or Detect Duplicate Elements in Array in Javajavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger27125tag:blogger.com,1999:blog-8712770457197348465.post-21494623971192791142015-11-14T09:30:35.500-08:002015-11-14T09:30:35.500-08:00public class FindDuplicate {
public static void...public class FindDuplicate {<br /> <br /> public static void main(String[] args) {<br /> Integer[] arr = new Integer[]{6, 2, 4, 4, 1,3,6};<br /> List duplicate =new ArrayList();<br /> Arrays.sort(arr);<br /> for(int a:arr)<br /> System.out.println(a);<br /> for(int i =0;i<arr.length-1;i++)<br /> {<br /> if(arr[i]==arr[i+1])<br /> {<br /> duplicate.add(arr[i]); <br /> }<br /> }Surinder Pal Singhhttps://www.blogger.com/profile/14806590205988385959noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65138746148827536172015-11-07T03:07:17.868-08:002015-11-07T03:07:17.868-08:00hi guys the problem given is to find duplicates in...hi guys the problem given is to find duplicates in a given array <br />// no need to know the complex stuff like hashset or brute stuff algorithm my code as follows<br />public class DuplicateElementsInArray{<br />public static void main(string args[])<br />{<br />int[] mynumbers = new int[] {1,3,5,4,1,2,3,5,4,7,6,7};//step1-i created an unsorted array<br />Arrays.sort(mynumbers);// step2- smartprasanthsunnyhttps://www.facebook.com/prasanth.sunny143noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-84415814012737404732015-10-11T03:09:43.014-07:002015-10-11T03:09:43.014-07:00@Anonymous, you can also use a hash table to solve...@Anonymous, you can also use a hash table to solve your problem, i.e. to find frequency of each word, any word with more than one count is duplicate, you can see code example <a href="http://java67.blogspot.com2015/10/java-program-to-find-repeated-words-and-count.html" rel="nofollow">here</a>javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-50449051543732735492015-10-10T13:21:58.169-07:002015-10-10T13:21:58.169-07:00U may use Arrays.asList(T...arr) to fetch list bac...U may use Arrays.asList(T...arr) to fetch list backed by that array and iterate/loop over that list to pass each element with list to Collections.frequency(list, obj) to get the frequency for that element. Store that with element as key, preferably to LinkedHashMap for order. U'll hv that map now keeping record of no of times each duplicate occursAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-26587626787986882722015-09-06T21:00:02.974-07:002015-09-06T21:00:02.974-07:00@Javin Paul
In an interview i were asked to
1)wr...@Javin Paul<br /><br />In an interview i were asked to <br />1)write a program to find how many times each duplicate occurred?<br /><br />can you please give me an idea to do this in a post or comment.<br /><br />Thank you.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-26036432967202413312015-08-19T00:11:00.293-07:002015-08-19T00:11:00.293-07:00@Samruddhi Jadhav
int[] duplicateArray = { 1, 2...@Samruddhi Jadhav <br /><br />int[] duplicateArray = { 1, 2, 3, 3,5,5 }; is returning 4 for duplicateArray. <br />Praffulhttps://www.blogger.com/profile/00013117538288113856noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-71465241797222490762014-10-06T23:02:35.430-07:002014-10-06T23:02:35.430-07:00Im having a conflict with using text file as input...Im having a conflict with using text file as input, containing the same array elements. The problem is that the output shows that both duplicate elements are considered false(ex. false output for one=one)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-79098248062903615782014-08-01T23:05:41.199-07:002014-08-01T23:05:41.199-07:00Hello, I found way to get duplicate number in arra...Hello, I found way to get duplicate number in array without using java API.<br /><br />public class DuplicateFinder {<br /><br /> public void isDuplicate(int[] inputArray) {<br /> int actualSum = 0, desiredSum = 0, difference = 0, duplicateNumber = 0;<br /> for (int i = 0; i < inputArray.length; i++) {<br /> actualSum = actualSum + inputArray[i];<br /> }<br /> desiredSum = (Anonymoushttps://www.blogger.com/profile/17648440488265270922noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-59813382699164373812014-06-24T00:44:59.040-07:002014-06-24T00:44:59.040-07:00I feel this can help
public class ArrayDuplicate...I feel this can help<br /><br /><br />public class ArrayDuplicates {<br /><br /> public static void main(String[] args) {<br /> int[] arrayValues = new int[4];<br /> Scanner sc = new Scanner(System.in);<br /> for (int i = 0; i < arrayValues.length; i++) {<br /> arrayValues[i] = sc.nextInt();<br /> }<br /> Set setValue = new HashSet();<br /> for (int i : arrayValues) {<br /> Vijaya Kumar Bathininoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-37408151384020086632014-06-20T07:28:42.370-07:002014-06-20T07:28:42.370-07:00Thanks for ur Answer.Its very helpful.Thanks for ur Answer.Its very helpful.R.JEYA NANDHANAhttps://www.blogger.com/profile/04560799779901413382noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-64354492818222660462014-06-11T13:19:15.045-07:002014-06-11T13:19:15.045-07:00Hey Guys ,how about this:
public static boolean ch...Hey Guys ,how about this:<br />public static boolean checkDuplicateUsingAdd(String[] input) {<br /> Map elementMap = new HashMap();<br /> for (String str : input) {<br /> Boolean wasInserted = elementMap.get(str);<br /> if (wasInserted!=null) {<br /> return false;<br /> }<br /> elementMap.put(str,Boolean.TRUE);<br /> }<br />Anonymoushttps://www.blogger.com/profile/18359002564532604419noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-59032433235835231492013-12-19T23:24:04.015-08:002013-12-19T23:24:04.015-08:00Java program to print integers which occurs thrice...Java program to print integers which occurs thrice in the array:<br /><br />package com.test;<br /><br />import java.util.ArrayList;<br />import java.util.Arrays;<br />import java.util.List;<br /><br />public class ArrayTest {<br /><br /> /**<br /> * @param args<br /> */<br /> public static void main(String[] args) {<br /> // TODO Auto-generated method stub<br /> <br /> int[] array = {Krishnahttps://www.blogger.com/profile/10127009673404171300noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-7316630881850612302013-07-04T15:11:24.628-07:002013-07-04T15:11:24.628-07:00but if it is " one" or " one "...but if it is " one" or " one " or "one " it will not be fund that's why i think we have to use trim if we suppose that " one" is equal to "one" ;thxxxx for the tuto ;)Anonymoushttps://www.blogger.com/profile/02192178984907476022noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-23390168546565626432013-06-30T09:34:15.081-07:002013-06-30T09:34:15.081-07:00public static boolean bruteforce(String[] input) {...public static boolean bruteforce(String[] input) {<br />for (int i = 0; i < input.length; i++) {<br />for (int j = 0; j < input.length; j++) {<br /><br />As RC said, j should start at i + 1 because if items at index 5 and 3 are the same, it will be found when i = 3 (and j = 5) so no need to look for it when i = 5 --> you only need to look forward, thus making the brute force method run Nat JMhttps://www.blogger.com/profile/15675840704348425518noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-15108670488388209612013-03-08T01:03:30.627-08:002013-03-08T01:03:30.627-08:00RC, you are correct, it can be like that, as Miche...RC, you are correct, it can be like that, as Michee already pointed out,but if you see I have put an extra condition i != j to not consider same element.<br />Javin @ Print array in Javahttp://javarevisited.blogspot.sg/2012/12/3-example-to-print-array-values-in-java.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-84555589975933264422013-03-07T12:39:10.614-08:002013-03-07T12:39:10.614-08:00In the brute force method, why are you going over ...In the brute force method, why are you going over the whole array again in the inner loop?<br /><br />j should start at i + 1, not at zero. Because all the elements before i + 1 have already been compared to the rest of the array.RCnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-8322533110919848132012-11-22T22:25:08.958-08:002012-11-22T22:25:08.958-08:00string is like this:"Hello abcdef ABCDEF 1234...string is like this:"Hello abcdef ABCDEF 1234 12AB"<br /><br />and I need o/p like this:Hello a-f A-F 1-4 1-B<br />please tell me if any one know.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-73742702005639866932012-11-09T05:41:47.033-08:002012-11-09T05:41:47.033-08:00Without using API you may sort the array with some...Without using API you may sort the array with some fast algorithm and do linear search for adjacent the same values.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-71069642539020528482012-03-08T02:16:01.710-08:002012-03-08T02:16:01.710-08:00if you don't want duplicate in array than conv...if you don't want duplicate in array than convert array into set. this way you don't need to check array for duplicates, because array backed up by Set doesn't contains repeated element. I guess this is best way to remove duplicates from array.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-91839937540476470432012-02-28T08:57:07.830-08:002012-02-28T08:57:07.830-08:00ps: my way it's more optimal!:Pps: my way it's more optimal!:Pmicheehttps://www.blogger.com/profile/05937230965190972040noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-87781678746372131102012-02-28T08:56:27.637-08:002012-02-28T08:56:27.637-08:00you're right i didn't see that 'i!=j&#...you're right i didn't see that 'i!=j' part :)<br />I don't do code review, i'm just a php dev learning java and i like it:)micheehttps://www.blogger.com/profile/05937230965190972040noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-85337591542242200462012-02-28T05:19:45.015-08:002012-02-28T05:19:45.015-08:00michee dude you are picking it very well, must be ...michee dude you are picking it very well, must be good in code review :) you are correct it should can start with i+1 but if you see I have put an extra condition i != j to not consider same element.Javin @ Sort array in javahttp://javarevisited.blogspot.com/2012/01/sort-array-in-java-ascending-and.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14403669843040265982012-02-27T08:33:48.601-08:002012-02-27T08:33:48.601-08:00Also in function bruteforce:
public static bo...Also in function bruteforce:<br /><br /> public static boolean bruteforce(String[] input) {<br /> for (int i = 0; i < input.length; i++) {<br /> for (int j = 0; j < input.length; j++) {<br /><br /><br />I think j should start from i+1 . Because every element will be equal to itself.micheehttps://www.blogger.com/profile/05937230965190972040noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-57470939823155948462012-02-27T05:17:19.373-08:002012-02-27T05:17:19.373-08:00@michee, you pointed right if condition was incomp...@michee, you pointed right if condition was incomplete it should be inputSet.size()<inputList.size(). Corrected. Thanks for pointing it out.Javin @ google interview questionhttp://javarevisited.blogspot.com/2012/01/google-interview-questions-answers-top.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-47486096361825086962012-02-27T00:42:09.760-08:002012-02-27T00:42:09.760-08:00followup questions most likely will be:
How to fi...followup questions most likely will be:<br /><br />How to find duplicate items in Array? (Actual item not just confirmation that array contains duplicate)<br />How to remove duplicates in Java Array?<br />How to find count of duplicates in Java array ?(e.g. how many times a particular element is appearing in array)Anonymousnoreply@blogger.com