Equal elements in array

Hi guys, I need to write a program that finds the longest sequence of equal elements in an array. For example: {7, 0, 4, 4, 0, 2, 2, 2} -> {2, 2, 2}. I've been trying tons of things and never get the correct result :(
Here's my buggy code:
int[] arr = {1, 3, 3, 3, 3, 8, 6, 6, 6, 9};
          int numberIs = 0;
          int count = 1;
          int cTemp = 1;
          int numTemp = 0;
          for (int i = 1; i < arr.length; i++){
               if (arr[i] == arr[i - 1]){
                    numTemp = arr;
                    cTemp++;
               if (arr[i] != arr[i - 1]){
                    if (arr[i] == arr[i - 1]){
                         numberIs = arr[i];
                         count++;
               if (cTemp > count){
                    count = cTemp;
                    numberIs = numTemp;
          System.out.printf("Number is %d; repeated %d times", numberIs, count);
I have a general idea what's wrong, but every solution I come up with fails... Need some help PLEASE, as I'll need this code working by March 31.
P.S. since I'm still a noob, I don'want any import declarations, trying to keep it as simple as possible.

You need a groupLengthCounter for keeping track of how long the current group you're examining isThat's what the int cTemp does.
You also need a max for keeping track of how long the longest group you found so far.I tried to do that with int count
Maybe also one to keep track of what the number was that made up that group (3 in your example)That's accomplished by int numberIs & int numTemp, compared to each other later
Needs some work to make sure array[i+groupLengthCounter] stays within bounds, but I'll leave that to you to figure out.One of the biggest problems I've had so far, couldn't resolve it on my own. And believe me, I tried many times. Nothing yet... Because of the outOfBounds excepion I would get I use the array index i like this: (int i = 1; i < arr.length;...) and if (arr[i] == arr[i - 1]) making sure I'm within bounds.
Arrays are my worst nightmare in Java :(

Similar Messages

  • Sort array with equal elements

    Hi, I'm a new one.
    I have a problem to sort 2d array with equal elements (it has 5000000 of elements from 1-255) for example:
    1 2 1 1 3 4 5 5 6 1 2 3 (value from 1-255)
    2 2 1 1 3 3 3 3 4 4 4 1 (value from 1-255)
    And I need make array or matrix 255 rows vs 255 colomns, and count how many equal elements for example:
      1  2 3 4 5 6
    1 0 1 0 1 0 0
    2 0 1 0 1 0 0
    3 1 0 1 0 0 0 
    4 0 0 1 0 0 0
    5 0 0 2 0 0 0
    6 0 0 0 1 0 0
    I'll be happy if someone could help me

    I understand that it's very complicated explain.
    I build 2d array from two 1darray each of them has values 1-255. So right now i must constract array or matrix 255x255 where must calculat how many times value from (1-255) for one array in another array and write this sum for each value 1-255. I try to show it
    1 2                         so I need array 3x3; for first row for value 1 I have two times value 3 and one time value 2
    2 2                                                                                    2 I have two times value 1 and one time value 2 and one time value 3
    3 2                                                                                    3 I have two times value 2 and one time value 3 
    3 2              so my result matrix will be      1   2  3
    3 3                                                       1  0  1  2
    2 3                                                       2  2  1  1
    2 1                                                       3  0  2  3
    2 1
    1 3
    1 3
    Maybe its more clear

  • Merging elements of array

    Hi,
    I am new to programming.
    I have a sensor that sends a data stream through a serial port. I have collect all that data in a 1D unsigned byte array. Now, lets say my 1-D array is [12][45][65][12]...... where each "[ ]"  is a element of the array. Now to make sense of this data what i need to do is merge two elements the array and multipy it by 2/(2^15). i.e. My converted data should by [1245]*2/(2^15). Basically 2 bytes together makes sense. I can do a simple add of each element. Also the other problem is,data from the sensor is a stored as the 1-D array  of unsigned byte. However, the converted data is supposed to be signed 16-bit integer.
    Please give any suggestions on how to go about doing this.
    Siddhant Shah

    Another option is "decimate&Join". (Here it is easy to change the byte order if needed).
    Message Edited by altenbach on 12-15-2008 06:28 PM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    8bitTo16BitByteOrder.png ‏5 KB

  • The prefix mx for element mx:Array is not bound

    Hi All
    I have a component:
    <?xml version="1.0" encoding="utf-8"?>
      <mx:Array id="arr">
        <mx:Object label="Flex"
          thumbnailImage="http:/someURL"
          fullImage="http://someURL" />
      </mx:Array>
    <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml">
    </mx:Canvas>
    This gives me an error
    The prefix "mx" for element "mx:Array" is not bound
    I need a root element here, but I cannot use mx:Canvas, what would be the best choice?
    cheers
    Luca

    Hi Luca,
    Try to put the Array inside your canvas rather than outside ...
    <?xml version="1.0" encoding="utf-8"?
    <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Array id="arr">
        <mx:Object label="Flex"
          thumbnailImage="http:/someURL"
          fullImage="http://someURL" />
      </mx:Array>
    </mx:Canvas>
    Why  you dont want to use mx:Canvas as root tag of your component.. ?? However which ever component you use try to put the <mx:Array/> inside your root tag of your component, otherwise you will be thrown the same error as mentioned. The error occurs as the the compiler doesn't recognize the namespace mx for the array since you declared it outside the root tag of your component.
    Thanks,
    Bhasker

  • Remove element from array

    hi
    if i have an array
    eg
    int[] array = {4,2,1,2,1};
    how to i remove the index 2 from it and create a new array?
    thanks

    You can't remove the element--an array's size is fixed at creation.
    If you want a new array, create a new one that's one element smaller, then use System.arrayCopy to copy the elements you want to keep.

  • How can i retrieve missing element in array

    how can i retrieve missing element in arrays if array length is exceeded

    Not sure about what the question means, but could it be that OP is not aware that the first element in an array is at index 0 (not 1) ?
    <a href="http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html" style="background:url(http://java.sun.com/docs/books/tutorial/figures/java/objects-tenElementArray.gif) no-repeat; width:400; height:145;"></a>

  • Get the number of elements in Array

    Hi everyone,
    Could anyone give me idea on how to get the number of elements in an Array (not number of rows) ?
    I tried to find that in both property node and array functions but nothing.
    Thanks,
    -Kun

    You are going to have to provide some proof that the Array Size does not work correctly. Right click on the input to it and select 'Create Indicator'. Run the VI. Go to the front panel and select 'Edit>Make Current Values Default'. Save the VI and attach it to your post.
    You can also check the array size yourself. Right click on the index display of the array and select 'Advanced>Show Last Element'. I will bet you that number shown is equal to Array Size -1.
    p.s. The number of elements in an array is equal to the number of rows in a 1D array. If you define the number of elements as anything else, you are mistaken.
    Message Edited by Dennis Knutson on 04-21-2009 10:40 AM

  • Access Array Elements by Arrays Reference

    Hi there,
    I'm currently working on a vi that writes clusters as recordsets into
    specific tables in a database.
    Due to this I have a problem filling an array of clusters equal to the
    recordset structure of
    a table. I wonder if it might be possible to access each element of an
    array by using the arrays
    reference instead of a local variable. At the moment I couldn't find any
    properly working method for
    that. Is anybody able to giv me some advice or hint?
    Thanks a lot!
    Sebastian Dau

    Sebastian; Be careful about thinking that you have solved your problem because a technique works--or appears to work. The method you are talking about uses excessive resources, is slow and inefficient and is an open source for race conditions. Right now the thing seems to work, what happens when you have other code executing at the same time, accessing the same data? Or say you install the code on a computer that's faster--or slower--than the one you're using now. Will it still work? If not will you be able to figure out why it suddenly stops working? Or why it only seems to work on certain computers? In terms of performance, you always have to go for the best performance you can because you never know how you will want to use the code in
    the future. In other words, you use this VI in one place and you can't notice the performance difference. But what happens when you use this VI in a 100 places in your code? Will the performance loss still be unnoticeable? Mike...
    Certified Professional Instructor
    Certified LabVIEW Architect
    LabVIEW Champion
    "... after all, He's not a tame lion..."
    Be thinking ahead and mark your dance card for NI Week 2015 now: TS 6139 - Object Oriented First Steps

  • How to removie a duplicate element of array

    i have this code so far can someone please help
    public static int [] remove(int[] a, int[] b){
    for(int i = 0; i < a.length; i++) {
    if(a[i] == b) {
    int[] newInt = new int[a.length - 1];
    return newInt; // We need something here
    return a;

    First, please use [ code] and [ code] tags to denote code; it makes it much more readable.
    Second, your arguments for the function don't make sense with relation to your subject. So, running on the asumption that I know what you mean through the subject, here goes:
    You probably want the arguments to be:
    (int[] a, int b)Whereby you want to remove the element that equals b . So, what you would want to do in such a situation is:
    for(i = 0 to a.length) {
    if(a[i] == b) {
      //You have reached the element to throw away.  Set a flag saying you have reached this point
    // If the flag is set, add a[i+1] to the new array; otherwise add a[i] to the new array.
    return a;

  • How to change a specific element in array

    hi 
    i have an 1d intialized array
    i want to take the existed array and for example add +1 for the 5th element
    how can it be done?
    thanks
    Solved!
    Go to Solution.

    In a typical application you would want to repeat that operation to form a simple histogram. This is most easily achieved by keeping the array in a shift register and repeating the operation in a FOR loop until all data is processed.
    My very simple example from a few years ago can be found here. In this case we are counting the number of occurences of small integers in a 1D array. Modify as needed.
    LabVIEW Champion . Do more with less code and in less time .

  • Web service proxy receiving single element in array

    I've generated a web service proxy over a web service and expect to get multiple elements returned from the web service. However when I excute the proxy I receive only the first element. Testing the web service with the same parameters on the application server shows that I should be receiving 3 elements in total. Not sure where the disconnect is in generating the response message within the proxy. Any suggestions is appreciated.

    Are you looking for SOAP ENC Array feature? Which version are you using 10134 or 11g?

  • Dynamically create empty mcs and asign elements from array and loadmovie

    I'm creating an educational game for my school students.
    A little boy is flying through the city when he encounters objects flying from left to right.
    He hears a SOUND eg: Dog - he must go and click the dog image with the flying cursor. There are at least 5 DIFFERENT objects that should be flying on the screen. There could be various of them at any one time.
    I have the roots of the images in an xml file. And the actual swf are in a file called IMAGE and the sounds in SOUND.
    My problem is that most tutorials I see use the attachmovie method but I don't want to put all the swf's in the library as there are hundreds.
    I have to use the loadmovie method.
    I take it I have to loop through the array and assign each element to an empty movieclip which in turn is in the loop so you get 5 empty clips - I will use i (index). It doesn't seem to be working. I shall keep trying and post back here if I get any luck but I'm running out of ideas.
    Then the objects have to float across the screen. Don't know whether to use tween object or onEnterFrame handler or other. AND someone has mentioned using setinterval to "spit out" the objects.
    BUT if I have five flying across the screen I'm left without clips to stick in any more.
    Oh my head hurts but I will keep going.
    CHEERS if any help is around. This should be quite a standard thng for game developpers. Code at the moment
    function loadEnemies():Void {
    enemy_xml = new XML();
    enemy_xml.ignoreWhite = true;
    enemy_xml.onLoad = function(success:Boolean) {
    if (success) {
    _root.parseEnemyXML();
    //enemy_xml.load("level_"+level+".xml");
    enemy_xml.load("data/animal_catch.xml");
    function parseEnemyXML():Void {
    rows = enemy_xml.firstChild.childNodes.length;
    for (var i:Number = 0; i<rows; i++) {
    var row_string:String = String(enemy_xml.firstChild.childNodes[i].firstChild.firstChild);
    _root["row_"+i+"_array"] = row_string; //MAIN ARRAY holds an array images/dog_a.swf/ images/cat_a.swf etc... all five
    _root.createEmptyMovieClip("enemyObjects", 1);
    enemyObjects.createEmptyMovieClip("holder_"+i, i);
    _root["object"+i] = new Sound(enemyObjects["holder_"+i]);
    trace(row_string);
    loadMovie["row_"+i+"_array"], ["holder_"+i]
    if (level == 1) {
    alerts_mc.play();
    } else {
    currRow = 0;
    rowCounter = 0;
    OK got to about here BUT
    a. I started to get confused around the createEmptyMovieClip part
    b. I KNOW I shouldn't have Sound(enemyObjetcs etc... BUT I copied the code from a tutorial and I don't know what to replace it with.
    I'm close but I need a little polishing.

    It doesn't do you much good to work with borrowed code that you do not understand.  Your best bet will be to start small, creating one functional piece of the puzzle at a time, and work your way up.  Start with making sure you are loading and parsing the xml properly, then set about loading the external content, then see about making that content move around, etc...
    In the code you show, your loadMovie line of code does not resemble anything I have seen before, looking more like (but not quite like) a multi-dimensional array element than a loadMovie() function call.  If you find you need to have control of the items as soon as they load, then you should consider using MovieClipLoader.loadClip instead of loadMovie.  The MovieClipLoader class provides features, such as to be able to determine when items have fully loaded.

  • Add element in array

    Hi,
    I have one String array.
    I need to create another String array with the content of 1st array and additional 1st element in the 2nd array with "Product" text.
    2nd array[0] = Product;
    How do i do it
    Thanks

    String[] array1 = {"1", "2", "3"};
    String[] array2 = {"4", "5"};
    String[] array3 = new String[array1.length+array2.length];
    System.arraycopy(array1, 0, array3, 0, array1.length);
    System.arraycopy(array2, 0, array3, array1.length, array2.length);
    here, the array3 will have all the elements in order ie, 1,2,3,4,5.
    In case you need to use array2, assign array3 to array2 ie, array2 = array3
    Hope that helps.
    Regards,
    Sumant

  • Sort Second Element in Array of Strict Type Def Clusters

    I need to sort the an array of strict type def clusters by the second or third element in the cluster.  In the past I have used 1-D array sort and sorted by the first element of the cluster.  When I need to sort by the second, third, or fourth element of the cluster I just break down the cluster and rebuild it placing the desired sort element in the first position of the new cluster.  This works for simple tasks and clusters, but as the cluster gets more complicated this becomes a pain to do this.  There must be a cleaner way to sort by other elements within the original array of strict type def clusters.  Has anyone succeeded in doing this a different way?

    Hello,
    Here's the way I would do it...just create a new cluster array, where each cluster contains two elements...the unbundled item you want to sort by, and the cluster itself.  Then sort the new cluster array, then unbundle the sorted cluster array to get the original clusters (now sorted).  Here is a screenshot:
    There may be a better way, but this is the first thing I thought of.
    -D
    Message Edited by Darren on 03-16-200610:00 AM
    Darren Nattinger, CLA
    LabVIEW Artisan and Nugget Penman
    Attachments:
    Sorted_cluster.jpg ‏30 KB

  • How to add all element of array?

    1-D array has N number of element .i want to sum all the element ,and the result is a number.
    can you give me some example?
    thanks.

    Just use the "Add Array Elements"-function located in the Numeric palette.
    Message Edited by becktho on 02-21-2006 12:58 PM
    Using LV8.0
    Don't be afraid to rate a good answer...
    Attachments:
    AddArrayElements.PNG ‏14 KB

Maybe you are looking for

  • How to close email notifications to senders from HP eprintCenter?

    Hi, How to close email notifications to senders from HP eprintCenter? Thank you.

  • Jdbc adapter: DeliveryException error

    Hello I have the following synchronous scenario: RFC <-> JDBC Adapter. The RFC is executed many times in a job and 99% of them works well, but some of them I receive the error: Returning synchronous error notification to calling application: com.sap.

  • Startup disk full...when it shouldn't be?

    for the last week i have barely been able to use more than one application at a time. i am often unable to save things on word and when i restart my computer i am told the startup disk is almost full. i've looked and the computer certainly thinks the

  • ITunes won't show shared library on Mac

    So, I have a network of a Mac Mini G4, a PC desktop with Win XP, and a netbook with Win XP. I have iTunes running on each machine - iTunes 10.0.1 on the two Win XP machines, and iTunes 9.2.1 on a Mac Mini G4 with OS X 10.4.11. I have authorized each

  • Crtl e  or  edit in - CS5 Photoshop - stopped working

    Working on an image in Lightroom 4, pressing ctrl e (or edit in-CS5 Photoshop) opens that image in Photoshop - only it doesn't?! Am not aware of having changed anything between it working fine and not. Rebooting (Win Vista) and restarting LR made no