This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

finding Repeated elements in an array...  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone...
I'm trying to find repeated elements in array, and this is my code:


The question is i just wanna to show just repeated elements;
for e.g i enter this number {1,1,2,2,3}
the out put is:
Repeated numbers are:  1:2,  1:2,  2:2,  2:2,  3:1
i just want to show 1:2, 2:2, 3:1

what's solution?

tnx for your help...
 
Marshal
Posts: 63391
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain what you are doing. Your desired output

1:2, 2:2, 3:1

doesn't show repeated elements, since 3 appears only once.
 
Alex Moradi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look,
1 repeated twice
2 repeated twice
3 repeated once
I wanna the output look like above
But i got that output:
1:2, 1,:2, 2:2, 2:2, 3:1

What change my code needs to get that output?
 
Campbell Ritchie
Marshal
Posts: 63391
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alex Moradi wrote:Look, . . . What change my code needs to get that output?

That isn't an explanation of what you want, simply an example. From which it appears you are trying to count values, not seek repeats.
 
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is he want's to loop his array and at the end print how many times each number occurs in the array.
 
Campbell Ritchie
Marshal
Posts: 63391
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that would constitute counting elements.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a limit of the value that's bin entered, otherwise it's gonna be hard to do with an array? for this example let's say all numbers are between or equal to 0 and 5. If your set on using an array, cause that would be easier with a list,  I would create a counter for each value, loop the array and use a switch to increment the counter. Then at the end you can use your counters to print the occurences of each value
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason why you getting not the desired output is cause your still in a for loop when printing it. You'll need a counter for each value, not a single counter that you set to zero each time
 
Campbell Ritchie
Marshal
Posts: 63391
205
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is awkward to count things like that without something to record the counts. There is an example in the Java™ Tutorials, but that may be at a later stage than OP has reached.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also I guess you entered your values on the same line seperatted with a space? Keep in  mind when the user tries to asign the values on seperatted lines,  the assignment of your array will fail, so eather warn the user not to do that or account for that mistake
 
Campbell Ritchie
Marshal
Posts: 63391
205
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure about that? I think your Scanner will be able to compensate for input on any number of lines, as long as you don't use nextLine(). Remember that the line ends between consecutive lines count as whitespace, so they are included in the default delimiter.
 
Bartender
Posts: 5635
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if "1 1 2 3 1 1" is entered? Do you want "1:4 2:1 3:1" ?

If so, there are a couple of approaches:
  • Sort the array first and then traverse in order.
  • Use a Map<Integer,Integer> where key is number and value is count.
  • Maintain a parallel boolean array to track which numbers you've already accounted for.
  •  
    Campbell Ritchie
    Marshal
    Posts: 63391
    205
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think we are going to have to wait for a response from OP before we can do any more.
     
    Ranch Hand
    Posts: 55
    2
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    With what you have now, create an empty Set<String>, fill it with whatever you are generating on line 20, print out elements of the set  
     
    Greenhorn
    Posts: 1
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    It works fine for your problem.
     
    Campbell Ritchie
    Marshal
    Posts: 63391
    205
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to the Ranch

    How do you know it works? OP hasn't told us, so we don't actually know what the problem is. It might be counting, but that looks a very complicated way to count things. Running two Lists in parallel is very error‑prone, so please explain all the details of how your code works.
     
    Adrian Grabowski
    Ranch Hand
    Posts: 55
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What about my noobish solution?

     
    Carey Brown
    Bartender
    Posts: 5635
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The use of frequency() is not particularly efficient in solving this problem. It will be called for each occurrence of '10' for example, coming up with the same count each time. You would only need to do this once for each unique value.
     
    Alex Moradi
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tnx to all for the answers.

    I have an array with 5 elements.

    I want to count repeated element, for e.g user input this set {1,1,1,2,3}

    The program must say in your set
    Number 1 repeated 3 times and ignor numbers that not repeated in the set!
    I don’t want print 2,3! //is it okay till now!?

    An other e.g user input this set:{2,2,0,4,4}
    The out put should be :
    2:2 , 4:2;
    0 not repeated and no need to going print !
    I don’t want to use hash table & map
    Just using an array and loops.

    I hope understand my mean.
    Tnx alot🤨
     
    Sheriff
    Posts: 13155
    219
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What you are asking is possible but I don't understand why you would limit yourself that way. The simplest and most straightforward solution is to use a map to create a histogram.

    Without a map, you will need to set up a different array to hold the duplicate counts. You will also need to have some logic that will skip elements that have been determined to be duplicates and somehow flag them so you can avoid recounting them.
     
    Campbell Ritchie
    Marshal
    Posts: 63391
    205
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Alex Moradi wrote:. . . Number 1 repeated 3 times and ignor numbers that not repeated in the set!  . . .

    That means, 3 appears 3×. I am afraid I am going into pedantic mode. You don't have a set there, beause sets don't contain duplicates; as a set, that would read {1, 2, 3}. What you are doing is counting elements equal to each other and only printing which are repeated.
    Did you see the link to the Map interface in the Java™ Tutorials I posted a couple of days ago? Here it is again.
     
    Greenhorn
    Posts: 6
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    2 solutions: one with java streams and second with arrays and loops.

     
    God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
    Become a Java guru with IntelliJ IDEA
    https://www.jetbrains.com/idea/
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!