关闭

Facebook面试题 Remove duplicates from array where all duplicate numbers are located adjacently

标签: 面试题facebook
221人阅读 评论(0) 收藏 举报
分类:

Given an integer array which has the property that all the elements haveing same value are adjacent to each other.
e.g. {2,2,2,1,5,6,6,7,7}, output: Remove duplicate element in place and output the length of the new array. Do it inplace and we don’t care about the elements outside the new length of the array.
e.g. return 5 and the input arrya will become {2,1,5,6,7,x,x,x,x}. “x” means we don’t care about its value.

  • Analysis:

    A brute force method for this question would be to traverse the array and store existed numbers into a HashSet on the fly. Each time we encountering a new number, put it to the last position of the new array.
    However, in order to solve this question in place, we must not store all prvious existed numbers. Notice that in the requirement mentioned that all the duplicates are adjacent to each other. Thus we only have to keep one variable for the previous number. Each time we encounter at a number, set the previous number variable to this new one and swap the number with the last position of the new array. By this way we can do this in place.

    The Time complexity of this algorithm is O(n) and the space complexity is O(1). Because we only traverse the whole array once and only use one variable.

The Java implementation is showed as following:

public class Solution {
    public int removeDuplicates(int[] array) {
        if (array == null || array.length == 0) return 0;

        int prev = array[0];
        int last = 1;

        for (int i = 1; i < array.length; i++) {
            if (array[i] != prev) {
                prev = array[i];
                swap(array, last, i);
                last++;
            }
        }

        return last;
    }

    private void swap(int[] array, int i1, int i2) {
        int tmp = array[i1];
        array[i1] = array[i2];
        array[i2] = tmp;
    }
}
0
0
查看评论

Remove Duplicates from Sorted Array [python解]

题目要求:Givena sorted array, remove the duplicates in place such that each element appearonly once and return the new length. Do not allocate e...
  • nadizhong4136
  • nadizhong4136
  • 2017-07-24 19:18
  • 265

26. Remove Duplicates from Sorted Array (python)

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space f...
  • qq_28119401
  • qq_28119401
  • 2016-10-30 15:58
  • 644

Remove Duplicates from Sorted Array -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/  这道题跟Remove Element类似,也是考察数组的基本操作,属于面试中比较简单的题目。做法是维护两个指针,一个保留当前有...
  • linhuanmars
  • linhuanmars
  • 2014-02-27 05:12
  • 10903

LeetCode:Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate ex...
  • fytain
  • fytain
  • 2013-09-02 20:42
  • 2757

LeetCode(26)Remove Duplicates From Sorted Array

题目如下 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra spa...
  • feliciafay
  • feliciafay
  • 2013-12-31 08:37
  • 1245

LeetCode 26 — Remove Duplicates from Sorted Array(C++ Java Python)

题目:http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the duplicates in place such that each element ...
  • dragon_dream
  • dragon_dream
  • 2014-02-23 15:12
  • 3851

Remove Duplicates from Sorted Array II -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates from Sorted Array比较类似,区别只是这里元素可以重复出现...
  • linhuanmars
  • linhuanmars
  • 2014-04-23 08:39
  • 10682

LeetCode(80) Remove Duplicates From Sorted Array II

题目如下: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], ...
  • feliciafay
  • feliciafay
  • 2015-01-11 03:55
  • 1295

Leetcode_26_Remove Duplicates from Sorted Array

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41521063 Remove Duplicates from Sorted Array Given a sorted array, remove the d...
  • pistolove
  • pistolove
  • 2014-11-27 23:21
  • 1163

Leetcode[26]-Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for...
  • Dream_angel_Z
  • Dream_angel_Z
  • 2015-06-09 10:22
  • 860
    个人资料
    • 访问:94866次
    • 积分:1517
    • 等级:
    • 排名:千里之外
    • 原创:84篇
    • 转载:1篇
    • 译文:4篇
    • 评论:6条
    文章分类
    我的GitHub