Var ListName=[5, 6,7,8,9,10,12,1,2,3,4]
Assuming, this list is hidden from me and I was asked to find the index of the element 9 and the next two elements that follows. Please how do I go about it.
I haven't tried anything because I'm new to list
Related
enter image description here
Hi there
I am new to photon and id like to compare the elements of two lists and get a list with now duplicates in the second function. however, my output only gives me a list with one element. Could somebody tell me what I am doing wrong?
Kind regards
I am populating a std::list<item*> itemslist from a text file, but there are instances where the name variable is the same, in those instances I want to remove the duplicated item (the item with the same name, other information of the item is irrelevant) but get data from that object to add to the 'original' (the first instance of the item)
Because I want to be able to load multiple information, aka an array of sorts into 'items' and this is what I have so far come up with
I tried to loop through items, then loop again and check the first loop against the second loop, copy the information and add to the array of the first loop item but it just breaks.
Suggestions?
Similar to this: Removing duplicates in a vector of strings
Except I want to use the information found in any duplicate and add it to a std::list that the item object holds
So if I load this:
Set Normal DIfficulty|Sets your save game to normal difficulty|/JKSV/Saves/Fire_Emblem__Awakening/hack/Chapter0|0x0D|0x00
Set Normal DIfficulty|Sets your save game to normal difficulty|/JKSV/Saves/Fire_Emblem__Awakening/hack/Chapter0|0x0F|0x0A
Set Hard DIfficulty|Sets your save game to hard difficulty|/JKSV/Saves/Fire_Emblem__Awakening/hack/Chapter0|0x0D|0x01
Set Lunatic DIfficulty|Sets your save game to lunatic difficulty|/JKSV/Saves/Fire_Emblem__Awakening/hack/Chapter0|0x0D|0x02
Set Lunatic+ DIfficulty|Sets your save game to lunatic+ difficulty|/JKSV/Saves/Fire_Emblem__Awakening/hack/Chapter0|0x0D|0x03
It will only have 1 Set Normal Difficulty item but that item will hold the duplicates last 2 pieces of information (aka 0x0F|0x0A
If you want your std::list<T> to erase duplicate values you have to call unique member function. There are two overloads of it. First take 0 arguments and compares values using operator==. It is not your case cause your list contains pointers and you don't need to compare addresses but name members. So try a second one. It takes a binary predicate where you can compare members
std::list<item*> myList;
// populate your list..
// sort it before calling unique..
myList.sort([](const item *lhs,const item *rhs)->bool{
return lhs->name < rhs->name;
});
myList.unique([](const item *lhs,const item *rhs)->bool{
return lhs->name == rhs->name;
});
// now your list has no duplicates..
More info here.
In one of the interviews, I asked by one of the interviewers how to implement history of web browser but don't show duplicates and need to show in reverse order meaning from most recent till the 5th website visited.
I told we can use linked list. When user enters a website it will be checked against a list of nodes and if the site is already present in the list, we will remove it from the list and add it as head. If it is not in the list, it will be simply added as head of the list. But he told order of complexity is O(n*n) and he asked me are there any other data structures or combination of data structured we could use to make the order of complexity as O(n). I didn't get any clue at that time. Can any one please let me know if you have any idea.
You could do it in constant time if you are using your linked list plus a hash table with pointers to the list items.
um...
With a linked list, Add URL to the start of the list (O(1)), continue throught list, deleting if found (O(n))
As others have mentioned, Using a list and an unordered map would be your best bet. When visiting a new page, if the user has never visited it, add it to the end. if they have visited, recall the iterator using the hash map and remove it. The last step is to then add the url to the start of the list and give it a fresh new iterator in the map.
#include <list>
#include <unordered_map>
class browser
{
public:
void visit (const std::string& page)
{
auto location = locations.find(page);
if (location != locations.end())
{
pages.erase(location->second);
}
pages.push_front(page);
locations[page] = pages.begin();
}
private:
using Pages = std::list<std::string>;
using Locations = std::unordered_map<std::string, Pages::iterator>;
Pages pages;
Locations locations;
};
I've got a spark list that gets dynamically filled.
So far the new items appear at the bottom of the list. What I'd like to do is to add them at the top.
The items in the list have a unique ID so some kind of sorting mechanism would probably do the trick as the new items have a greater ID than the old ones.
What I'd like to avoid is some complex method behind this as I'm working on a mobile platform and the list can get quite big so I need this to be as efficient as possible.
The list's data provider is an ArrayList that gets updated using binding.
How can that be done?
Thanks in advance!
u can Add the items at the starting index of the datagrid. Flex datagrid automatically renew all the indexes and add 1 to all existing element indexes. So
YourDataGridId.dataprovider.addItemAt(item,0) will do.
I have a CComboBox control with several items and I need to remove some of them, but the indexes of the remaining items should be preserved.
When the combo box is populated, the item data is set like so:
index = mycombo.AddString(temp);
mycombo.SetItemData(index, static_cast<DWORD>(count));
where count is a loop counter, and should be equal to index
Now I want to remove some of the items later, but I need the index of each remaining item to stay the same. Is CComboBox::DeleteString(UINT nIndex) what I should use? Its documentation says:
All items following nIndex now move down one position. For example, if a combo box contains two items, deleting the first item will cause the remaining item to now be in the first position. nIndex=0 for the item in the first position.
Is that talking about the physical location in the drop-down menu, or the index value associated with the item?
Is there another function that does what I need? Another solution altogether?
Is that talking about the physical location in the drop-down menu, or the index value associated with the item?
For a ComboBox (as well as ListBox, List Control, and probably many other such things) the location of an item on the control is directly tied to its index. The index is the location. Really, just think of it as if ComboBox was implemented internally using a simple std::vector. You can't remove an entry from a vector without affecting the indexes of all subsequent entries, and it is just the same with these controls.
However, the Item Data of an entry in a ComboBox (and other such controls) sticks with that entry no matter what index it is reassigned to.
Say you created two entries: the first at index 0 has text="A" and ItemData=0; while the second at index 1 has text="B" and ItemData=1. If you then remove that first entry, the second entry will shift down the claim the index and its ItemData will travel along. So you would be left with a single entry at index 0 having text="B" and ItemData=1.
In a combobox you have items which have a string and an integer value associated. Normally, you just see the string. Those items are referenced by an index, which just represents the location of each item in the list. If you remove an item, all the items below it are "relocated", so the index changes. The same happens in you insert an element anywhere between two items, or at the beginning.
The index always goes from 0 to (number_of_items-1), and there's nothing you can do about it.
That said, the item data always stays with the item, and it's this what you should look at when looking for a specific item. Not its index, and neither its string. Look at the item data. The index can change if you add, remove or resort the items. The strings will change if you localize the software. So use the data to properly identify each element.
You can take a look at http://www.flounder.com/combobox.htm, where you can find a better explanation, with some examples and code to do work with comboboxes more easily.
Adding or removing items does not change the number you passed to SetItemData(). GetItemData() returns the same number. You however need to pass the index of the item to DeleteString(). When lower numbered items were deleted before then the index will no longer match GetItemData(). If you lost track of the index of a specific item you want to delete then you need to iterate the items to find it back.