| Autor |
Nachricht |
Geri1
Unregistrierter
|
Geri1 Unregistrierter
18:14:13 03.01.2013 Titel: |
remove a set of values in an array given a set of indexes |
Zitieren |
Hi,
what is the best way to remove a set of values in an array? The set of values to remove is given as an array of indexes.
I can think of several solutions, either O(n) or O(n logn) with different memory requirements
array = [12, 24, 50, 1, 4, 5, 6, 9, 24, 6, 7, 3, 23, 2];
Index_to_remove = [4, 2, 1, 13, 9, 6]; |
|
|
|
 |
cooky451
Mitglied
Benutzerprofil
Anmeldungsdatum: 16.10.2010
Beiträge: 6874
|
cooky451 Mitglied
18:26:52 03.01.2013 Titel: |
|
Zitieren |
If the order isn't fixed the fastest variant is probably to just swap the values to the end and than cut them off. |
_________________ Sie sind nicht berechtigt unrechtmäßige Kopien dieses Datenträgers zu erstellen.™
Keksverteilungsbeauftragter
|
|
 |
Michael E.
Mitglied
Benutzerprofil
Anmeldungsdatum: 25.10.2003
Beiträge: 5712
|
Michael E. Mitglied
18:51:14 03.01.2013 Titel: |
|
Zitieren |
Do you have to keep the order? If yes, then cooky's method does not work. But how can you calculate the result in linear time?
Edit: Sorry, cooky, should've read your whole post. |
_________________ Your password must be at least 18770 characters and cannot repeat any of your previous 30689 passwords. Please type a different password. Type a password that meets these requirements in both text boxes. (http://support.microsoft.com/kb/276304/en-us/)
Zuletzt bearbeitet von Michael E. am 18:52:46 03.01.2013, insgesamt 1-mal bearbeitet |
|
 |
camper
Mitglied
Benutzerprofil
Anmeldungsdatum: 06.08.2004
Beiträge: 5802
|
camper Mitglied
19:02:44 03.01.2013 Titel: |
|
Zitieren |
| cooky451 schrieb: | | If the order isn't fixed the fastest variant is probably to just swap the values to the end and than cut them off. | That works only if values located at the end are known to be kept. |
|
|
|
 |
Unregistrierter
|
Unregistrierter
19:08:21 03.01.2013 Titel: |
|
Zitieren |
Can you use special values in array to mark elements for removal (such as negative numbers, NaN, ...)? This way O(n) without additional memory would work. |
|
|
|
 |
cooky451
Mitglied
Benutzerprofil
Anmeldungsdatum: 16.10.2010
Beiträge: 6874
|
cooky451 Mitglied
19:18:08 03.01.2013 Titel: |
|
Zitieren |
Guys.. he doesn't have values, he already has the index of the elements. |
_________________ Sie sind nicht berechtigt unrechtmäßige Kopien dieses Datenträgers zu erstellen.™
Keksverteilungsbeauftragter
|
|
 |
Unregistrierter
|
Unregistrierter
19:29:16 03.01.2013 Titel: |
|
Zitieren |
|
 |
knivil
Mitglied
Benutzerprofil
Anmeldungsdatum: 11.02.2009
Beiträge: 5870
|
knivil Mitglied
21:37:12 03.01.2013 Titel: |
|
Zitieren |
| cooky451 schrieb: | | Guys.. he doesn't have values, he already has the index of the elements. | Maybe you can use unused bits in your "number". |
_________________ If it were not for laughter, there would be no Tao.
Sie können einen Beitrag nicht so schnell nach Ihrem letzten absenden, bitte warten Sie einen Augenblick.
|
|
 |
cooky451
Mitglied
Benutzerprofil
Anmeldungsdatum: 16.10.2010
Beiträge: 6874
|
cooky451 Mitglied
21:46:55 03.01.2013 Titel: |
|
Zitieren |
..what? |
_________________ Sie sind nicht berechtigt unrechtmäßige Kopien dieses Datenträgers zu erstellen.™
Keksverteilungsbeauftragter
|
|
 |
Unregistrierter
|
Unregistrierter
21:50:17 03.01.2013 Titel: |
|
Zitieren |
eg. bit 31 is never set because the input range (of array) is limited. So you can loop through Index_to_remove, set bit 31 for every index in array, then loop through array and remove the values with bit 31 set... |
|
|
|
 |