• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Find Duplicate in jagged array java

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to do this in jagged array java
finds the duplicate characters and prints their positions and number of occurrence on screen.
Using for and if
Map and hashMal not allowed
 
Bartender
Posts: 7428
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't heard of a "jagged" array. Java has arrays, and also arrays of arrays where the secondary arrays may be of differing lengths. What is your definition of jagged?

Can you post the code you've written so far and tell us where you're stuck?
 
Marshal
Posts: 70982
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . Java has . . . arrays of arrays where the secondary arrays may be of differing lengths. . . .

I have heard that sort of array of arrays called a jagged array. The opposite is a rectangular array where all the component arrays have the same length.
Actually, I don't think code for searching a jagged array is different from code for searching a rectangular array.
 
Mark Oliver
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where the secondary arrays may be of differing lengths
i have an output like this
How many characters in your first name? 3
How many characters in your father name? 5
How many characters in your grandfather name? 3

Enter character for position (0,0): a
Enter character for position (0,1): b
Enter character for position (0,2): c

Enter character for position (1,0): a
Enter character for position (1,1): s
Enter character for position (1,2): q
Enter character for position (1,3): a
Enter character for position (1,4): n

Enter character for position (2,0): m
Enter character for position (2,1): k
Enter character for position (2,2): c

finds the duplicate characters and prints their positions and number of occurrence on screen
 
Campbell Ritchie
Marshal
Posts: 70982
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, how would you count duplicates without a computer? Write down that process very simply, and that will make counting with a computer easier/
 
Marshal
Posts: 7847
538
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Actually, I don't think code for searching a jagged array is different from code for searching a rectangular array.


@OP this ↑
 
Liutauras Vilda
Marshal
Posts: 7847
538
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on the context what you potentially learning in a class, there might be differences in implementation when:
(a) array of arrays is square, meaning it has the same amount of rows and columns VS (b) when the amount of rows does not match an amount of columns

i.e.
(a)                                   (b)
{{1, 2, 3, 4},         {{1, 2, 3, 4},
 {1, 2, 3, 4},          {1, 2},
 {1, 2, 3, 4},          {1, 2, 3},
 {1, 2, 3, 4}}          {1, 2, 3, 4}}



hint: there is a gottcha in nested loop.
Staff note (Liutauras Vilda) :

(b)'s solution would work in both cases, while (a)'s wouldn't.

 
Campbell Ritchie
Marshal
Posts: 70982
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about this sort of array?
(a)                                   (b)
{{1, 2, 3, 4},         {{1, 2, 3, 4},
 {1, 2, 3, 4},          {1, 2},
 {1, 2, 3, 4},          {},
 {1, 2, 3, 4}}          {1, 2, 3, 4, 5}}
 
Bartender
Posts: 2730
133
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Oliver wrote:... Map and hashMal not allowed


Well there's LinkedHashMap and TreeMap if you want to troll your professor    
 
Liutauras Vilda
Marshal
Posts: 7847
538
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:

Mark Oliver wrote:... Map and hashMal not allowed


Well there's LinkedHashMap and TreeMap if you want to troll your professor    


Well, probably not, because they are both either HashMap or Map.
 
Campbell Ritchie
Marshal
Posts: 70982
291
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:. . . if you want to troll your professor    

. . . or if I want to move Salvin to MD
 
Sheriff
Posts: 15983
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Oliver wrote:
Using for and if
Map and hashMal not allowed


So the challenge really is thinking of an alternative data structure to hold the mapping of letter to count. If you consider the fact that each letter has a position within the alphabet, for example, 'a' has position of 0, 'b' has position of '1', etc, then you can just use another array to store the counts and use the letter itself to calculate the appropriate index within that array. That is, assuming your input consists only of the 26 letters of the English alphabet, if you have an int[26] count then count[0] would be the count for 'a' or 'A', count[1] would be for 'b' or 'B', and so on.

Also consider that in Java, char is an integer type. This means that you can do arithmetic with char variables. For example, you can do this (in JShell):

That is, adding 3 to 'b' yields the char 'e'.

Note that there is some nuance that you'll have to work through because any char value will be first promoted to int per Java's rules for automatic promotion when evaluating integer expressions. Play around with char and int arithmetic in JShell to see how you might calculate an array index from a char value.
 
Campbell Ritchie
Marshal
Posts: 70982
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . any char value will be first promoted to int . . . .

If you don't mind your programming style getting even worse, you can do this sort of thing:-
 
salvin francis
Bartender
Posts: 2730
133
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, you can also consider the method suggested here by Mike Simmons

It involves populating a 256 int array (let's call it characterCounts) with all 0s and then filling each slot with the number of occurrences as you find them. e.g. if you find 'a' you increment the value of characterCounts['a'] (or the 97th element in the array). Effectively using an array like a Map.

The only downside is the assumption that there are merely 256 characters in this world (pun intended)
 
Junilu Lacar
Sheriff
Posts: 15983
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:and prints their positions


Still the same data structure. You'd iterate over it and if the count for a particular element is more than 1, calculate the letter that corresponds to that element and then loop through the jagged array and print out the position of each occurrence of that letter.

Sure, it's not ideal in terms of performance but neither is the constraint to not use a Map. If you insist on having a constraint like that then you're going to have to pay for that decision.
 
Rancher
Posts: 3955
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can have an int array for counting, could you have a parallel String array to hold the locations where the value was found.  The locations would be concatenated to the current String for that character.
For example for 'a' the String would be:  "(0,0) (1,0) (1,3)"
 
Saloon Keeper
Posts: 4139
160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many solutions have been suggested (my idea was like Liutauras code).

But it is two days since OP replied, so I suggest we now wait for another reply from him. Curious whay he thinks of ir!
 
salvin francis
Bartender
Posts: 2730
133
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:If you can have an int array for counting, could you have a parallel String array to hold the locations where the value was found...


or simply use a Plain old java object. The question does not prohibit that
 
salvin francis
Bartender
Posts: 2730
133
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:and prints their positions

Yes, this would work as well with the array solution. The only way it would fail if you use an emoji or a character not in range of 256
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic