Results 1 to 2 of 2
- 04-07-2010, 05:51 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 13
- Rep Power
- 0
How to detect duplicate values in an Array?
Hi,
I need to find duplicate values in an array and get their indexes, by comparing every value with another.
my code is below:
the way i have searched the array doesn't compare every possibility. please helpJava Code:if array cotains: 0 1 1 0 1 int[] array = new int[5]; for(int i = 0; i<array.length(); i++{ for(int j=1; j<array.length(); j++{ if(array[i]==array[j]{ //store index i and j } }
- 04-08-2010, 07:58 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
1. You need a closing paren on the if and for statements.
2. length is a "property" not a method
3. The outer loop need only go to array.length - 1 (there is no reason to compare the last index to any other index when all the other indexes have already been compared to it)
4. Make the inner loop start a i + 1 rather than 1 (i.e. there is no reason to compare index 5 (outer) to index 1 (inner) when you've already compared index 1 (outer) to index 5 (inner)).
Similar Threads
-
Custom Validator for checking duplicate values with database
By tanalyw in forum Web FrameworksReplies: 1Last Post: 03-08-2010, 01:34 PM -
Error if array contains duplicate integers
By lithium002 in forum New To JavaReplies: 4Last Post: 12-05-2009, 08:58 AM -
Detect "on duplicate key" - Mysql
By Jazzperson in forum Java ServletReplies: 6Last Post: 05-31-2009, 01:17 PM -
Counting Duplicate Variables in an Array
By Npcomplete in forum New To JavaReplies: 2Last Post: 10-24-2008, 07:33 PM -
How to make a hashmap to allow duplicate values?
By Preethi in forum New To JavaReplies: 0Last Post: 02-08-2008, 12:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks