java - Finding Duplicate Array Elements -
I am struggling to create a function essentially to find all the indexes of duplicate elements in a multi-dimensional array I (Canceled), in this case a 5x5 array, and then using the index, the score is found by changing the parallel element in the array. But within the column, duplicates are not available only and do not compare other columns in the array comparatively, here's what I have done with online research. The main problem with this code is that it will find all duplicate elements But not original For example: if there are elements in the array: {{"a", "a", "a"}, {"b", "b", "b"}, {"a", "c", "a "}}, Then it should change the parallel score array: {{0,1,0}, {1,1,1}, {0,1,0}} but instead it only recognizes the previous line and the top Returns the duplicate of the line at the top.
code:
public static zero search duplicate (string A [] []) {System.out .println ("* duplicate *"); Set = new hashet (); For (int j = 0; j & lt; a.length; j ++) {for (int i = 0; i
I know that my explanation is a bit complicated, but hopefully it is understandable enough. Thanks, Jake.
Your argument is wrong. Your external loop is j
and the internal loop is i
but you are doing:
set.add (a [i] [j] ]];
It should be on the other side:
set.add (a [j] [i]); Technically you can get the range exception if the array is not NxN but you can say that as a precondition. For any reason, you are setting 0 as:
score [i] [j] - = score [i] [j];
Why not just:
scores [i] [j] = 0;
But to find duplicates in columns:
public static zero search duplicates (string A [] []) {for (int col = 0; Col & lt; a [0] length; colonel ++) {map & lt; String, integer & gt; Value = new hashmap & lt; String, integer & gt; (); {Integer current = values.put (a [row] [col], line) for (line number = 0; line; lt; a.length; row ++); If (current = null) {score [row] [color] = 0; Score [current] [color] = 0; }}}}
How does this work?
- I named the loop variable as
row
and col
. There is no reason to use i
and j
, when line
and col
are more descriptive; - Like you think that the input array is correct as a condition it can be NXM (instead of just NXN instead);
- I use
map
to store each value pointer. Map.put ()
returns the old value if the key is already in map
. If this is the case then you have got a duplicate; - The current (line, color) and (current, color) are set to 0. Why reduce the score by itself rather than setting 0?
- If the value "A" gets 3 + times in a column, then
scores [current] [color]
will be set to more than 0, which is unnecessary but Is not harmful and makes for simple code. - I have declared a
map
by using generic. It is useful and recommended that the string
in map
And the integer
is the value, which saves some casting; - This also uses automatic boxing and the wrapper class
integer
>
Comments
Post a Comment