finding the most repeated number in an array C++ - c++

Have to make program, where you input number, and program outputs most repeated digit in it, can't figure out have to do it. Tried some things, for me it works with static array, but I need dynamic, so I dont now what to do.
Can someone help me?

make an array in size of 10 (the number of digits)
run over your original array and extract from each number all its digit, increase the value of your digit array for each digit you find.
-find the maximal digit using the sight array.
You can upload a code if you want more help

I assume you are served in real time digits [0-9] and you need a function that at any given time returns you the most frequent digit seen so far. A simplest solution would be just to have a hash map of [0-9] keys that maintains the number of times every digit is seen. When you need the most frequent digit you iterate over the 10 keys and return the one with the biggest count.

Related

Filling leading space with Zero

I have account numbers and the leading zero's have been dropped. For example 01234567 is instead recorded as 1234567. How can I easily put all the zero's back in?
Thanks for the help.
You can use the Z format to print a number with leading zeros; you'd want z8. to print 8 digits with leading zeros as necessary.
A bigger problem is that your data is probably the wrong type. It's generally a bad idea to store things like phone numbers and account numbers as numerics. You should only store something as a numeric if you plan to do arithmetic with it. Anything else should be a character variable, even if it only consists of the digits 0-9.
If possible, create the variable as a character variable from the start. That will preserve the leading zeros and also prevent multiple possible headaches in the future.

Replace a specific word followed by sometimes single digit number or other times two digit numbers

Strings will always end with 'Row' followed by a number. For example,
desk_Row2.txt
desk_Row15.txt
If sorted, desk_Row15.txt will precede desk_Row2.txt.
If it's a single digit number, I want to put a leading 0 in front of it so that, when sorted, it will be:
desk_Row02.txt
desk_Row15.txt
I figured out a long way where I find 'Row' with findstr and '.' and what's between them is a number. Then I can figure out whether str2double(that) is greater than 9. Well, I think this can be done in a matter of one or two sentences.
More generally, I want to learn to create expressions so that I can do the above myself later on. For example, I have no idea what (^|\.)\s*. means.
I was thinking to use regexprep, but I have no idea what the expression should be.

Regular Expression (consecutive 1s and 0s)

Hey I'm supposed to develop a regular expression for a binary string that has no consecutive 0s and no consecutive 1s. However this question is proving quite tricky. I'm not quite sure how to approach it as is.
If anyone could help that'd be great! This is new to me.
You're basically looking for alternating digits, the string:
...01010101010101...
but one that doesn't go infinitely in either direction.
That would be an optional 0 followed by any number of 10 sets followed by an optional 1:
^0?(10)*1?$
The (10)* (group) gives you as many of the alternating digits as you need and the optional edge characters allow you to start/stop with a half-group.
Keep in mind that also allows an empty string which may not be what you want, though you could argue that's still a binary string with no consecutive identical digits. If you need it to have a length of at least one, you can do that with a more complicated "or" regex like:
^(0(10)*1?)|(1(01)*0?)$
which makes the first digit (either 1 or 0) non-optional and adjusts the following sequences accordingly for the two cases.
But a simpler solution may be better if it's allowed - just ensure it has a length greater than zero before doing the regex check.

How to find longest palindrome [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Write a function that returns the longest palindrome in a given string
I have a C++ assignment which wants me write a program that finds the longest palindrome in a given text. For example, the text is this: asdqerderdiedasqwertunut, my program should find tunut in the index of 19. However if input is changed into this astunutsaderdiedasqwertunutit should find astunutsa in the index of 0 instead of tunutin index of 22.
So, my problem is this. But I am a beginner at the subject, i know just string class, loops, ifs. It would be great if you could help me on this.
Thanks in advance.
The idea is very simple:
Write a function is_palindrome(string) that takes a string, and returns true if it is a palindrome and false if it is not
With that function in hand, write two nested loops cutting out different substrings from the original string. Pass each substring to is_palindrome(string), and pick the longest one among the strings returning true.
You can further optimize your program by examining longest substrings ahead of shorter ones. If you examine substrings from longest to shortest, you'll be able to return as soon as you find the first palindrome.
Dasblinkenlight's idea is pretty good, but it's faster this way:
A palindrome has either an even number of letters or odd, so you have two situations. Let's start with the even. You need to find two consecutive identical letters, and then check whether the immediately previous letter is identical to the next letter. The same in the other situation, except at first you only need one letter. I don't speak English that well, so I hope you understood. :)

Regular Expression to match only odd or even number

I have a list of textual entries that a user can enter into the database and I need to validate these inputs with Regular Expressions because some of them are complex. One of the fields must have gaps in the numbers (i.e., 10, 12, 14, 16...). My question is, is there a Regex construct that would allow me to only match even or odd digit runs? I know I can pull this value out and do a division check on it, but I was hoping for a pure Regex solution to this if possible.
[Edit]
The solution I ended up using on this was an adaption of JaredPar's because in addition to needing only odd's or evens I also needed to constrain by a range (i.e., all even numbers between 10-40). Below is finished Regex.
^[123][02468]$
Odd Numbers
"^\d*[13579]$"
Even Numbers
"^\d*[02468]$"
Run of Odds with a , and potential whitespace separator
"$\s*(\d*[13579]\s*,\s*)*\d*[13579]$"
Run of Evens with a , and potential whitespace separator
"$\s*(\d*[02468]\s*,\s*)*\d*[02468]$"
The Regex is actually not too hard to design, if you take into account that an even or odd number can be tested by only looking at the last digit, which need to be even or odd too. So the Regex for odd number runs could be:
"^(\s*\d*[13579]\s*,)*(\s*\d*[13579]\s*)$"
Replace [13579] by [02468] for even numbers...
Do you mean something like:
/(\d*[02468](, *\d*[02468]))|(\d*[13579](, *\d*[13579]))/
or one of the three other possible interpretations of your question as worded?